Skip to main content

How to print number series using different loops in C

This C Language program prints numbers from 1 to 5 using for loop, do-while loop, and while loop in easy steps.

Change the counter value in case the user wants to print n numbers.

#include<stdio.h>  
int main(){    
int i=1,j=1,k=1;      
//print number series using do while loop
do{    
printf("%d \t",i);    
i++;    
}while(i<=5);   
printf("\n");

//print number series using while loop

while(j<=5)
{
    printf("%d\t",j);
    j++;
}
//print number series using for loop
printf("\n");
for(int k=1;k<=5;k++)
  printf("%d\t",k);
return 0;  
}     

Output

1 	2 	3 	4 	5 	
1	2	3	4	5	
1	2	3	4	5

By changing variable values one can print n number series in C language using three different loops.

C program to find the average of user input using array

This program accepts users to input up to any number and stores it Array. Then calculate the average of stored numbers by calculating the total or sum and then dividing it by a number of inputs.

This C program uses an array to store scores or numbers in a declared array.

In for loop program ask the user to input numbers and store them in the array.

Once the loop is over code calculate the average.

//average of user input numbers or scores using array
#include <stdio.h>
int main() {
int arrayVal[100],cntScore,Total=0,i; 
float average;
printf("Enter No of Scores to Calculate Average\t");
scanf("%d",&cntScore);
for(i=0;i<cntScore;i++)
{
    printf("Enter %d Score",i+1);
    scanf("%d",&arrayVal[i]);
    Total = Total + arrayVal[i];
}
average = (float)Total/cntScore;
printf("Average of %d Score is %.2f",cntScore,average);
return 0;
}

Output

Enter No of Scores to Calculate Average	3
Enter 1 Score	11
Enter 2 Score	22
Enter 3 Score	33
Average of 3 Score is 22.00

Code is available to download from GitHub

https://github.com/malpuresuhas/Github_C_Program/blob/master/averageNumbers.c

How to get ASCII values of A to Z

ASCII stands for American Standard Code for Information Interchange. Like how we understand A to Z in simmilar way machine understand A to Z in some language. So let’s see what is ASCII value of A to Z which require to perform electronic communication. modern character-encoding schemes are based on ASCII.

To print ASCII value of A to Z in C Programming Language. ASCII contains fix integer value.

So in this program will print the A to Z and using %d we will try to print associated ASCII Value for each character. So to get below output here is complete c program code.

Output

ASCII Value of A is 65
ASCII Value of B is 66
ASCII Value of C is 67
ASCII Value of D is 68
ASCII Value of E is 69
ASCII Value of F is 70
ASCII Value of G is 71
ASCII Value of H is 72
ASCII Value of I is 73
ASCII Value of J is 74
ASCII Value of K is 75
ASCII Value of L is 76
ASCII Value of M is 77
ASCII Value of N is 78
ASCII Value of O is 79
ASCII Value of P is 80
ASCII Value of Q is 81
ASCII Value of R is 82
ASCII Value of S is 83
ASCII Value of T is 84
ASCII Value of U is 85
ASCII Value of V is 86
ASCII Value of W is 87
ASCII Value of X is 88
ASCII Value of Y is 89
ASCII Value of Z is 90
#include <stdio.h>
int main()
{
    char ascCh; //declaring variable which start from Alphabet A
    for(ascCh='A'; ascCh<='Z'; ascCh++)
    {
        printf("ASCII Value of %c is %d\n", ascCh,ascCh);
    }
    return 0;
}

How to generate the series 1, 3, 6, 11, 18, 29, 42? up to N number in C Program

To generate series 1, 3, 6, 11, 18, 29, 42? in C Language it is important to understand the logic behind it. If we observe the difference between each number is like 2,3,7,11. So these are prime numbers. To generate such a series it is important that the difference between numbers should be an incremental prime number. So below program will generate a series up to user input by having prime number difference.

// Online C Program to generate Series up to Number
// 1, 3, 6, 11, 18, 29, 42?
#include <stdio.h>
int main() {
    //variable declaration
    int i,j,num,primeFlag,seriesNum,Scount;
    printf("\n how many numbers you need in this Series 1, 3, 6, 11, 18, 29, 42? to Print\n");
    scanf("%d",&num);
    //for loop to print numbers
    seriesNum =1; //This variable to store number from series.
    Scount=1; //variable to check count of numbers in series.
    printf("\t%d",seriesNum);
    do
    {
        primeFlag=0; //flag to check if number is prime or not.
        i= i+1;
        //loop to find prime number and add it to previous
        for(j=1;j<=i;j++)
        {
            if((i%j)==0)
            {
                primeFlag++;
            }
            else
            {
               //continue
            }
        }
        if (primeFlag == 2)
        {
//here if number is divisible by 1 and self number then only primeFlag will become 2
            seriesNum = seriesNum + i;
            printf(" %d,",seriesNum);
            Scount = Scount + 1;
        }
    }while (Scount < num);
    return 0;
}
how many numbers you need in this Series 1, 3, 6, 11, 18, 29, 42? to Print
22
1 3, 6, 11, 18, 29, 42, 59, 78, 101, 130, 161, 198, 239, 282, 329, 382, 441, 502, 569, 640, 713,

Simple C program to get square root of any number

Square Root is a factor of a number that, when multiplied by itself, gives the original number. This C Program will find squareroot of any number. C language provides math.h library file to perform various mathematical operations like square, power.

For example, if we take an example of 25 then the square root is 5.
5*5 = 25

// Program for math operations like power, square root.
#include <stdio.h>
#include <math.h>
int main() {
    // declaring variable in double
    double num,sr;
    printf("\nEnter number to find square root\t");
    scanf("%lf",&num);
    sr = sqrt(num);
    printf("\n\nSquare root of number %lf is %lf",num,sr);
    return 0;
}

Output

Enter number to find square root	625
Square root of number 625.000000 is 25.000000

C Program to print Math tables up to n numbers

This c program prints math tables up to n number. The program accepts user input. Using for loop we are printing the math table vertically.
To achieve math table output check the below c output and code.

Output

Up to which number you need to print table      10

        1       2       3       4       5       6       7       8       9       10
        2       4       6       8       10      12      14      16      18      20
        3       6       9       12      15      18      21      24      27      30
        4       8       12      16      20      24      28      32      36      40
        5       10      15      20      25      30      35      40      45      50
        6       12      18      24      30      36      42      48      54      60
        7       14      21      28      35      42      49      56      63      70
        8       16      24      32      40      48      56      64      72      80
        9       18      27      36      45      54      63      72      81      90
        10      20      30      40      50      60      70      80      90      100
//Program to display math table up to n number vertically
#include <stdio.h>
int main () {
  int tNo,i,j;
  printf("\n\tUp to which number you need to print table\t");
  scanf("%d",&tNo);
  for(i=1;i<=10;i++)
  {
      printf("\n");
      for (j=1;j<=tNo;j++)
      printf("\t%d",j*i);
    }
   return 0;
}

C Program to calculate total Body Mass Index BMI with weight and height

Body Mass Index is a simple calculation using a person’s height and weight. To calculate BMI we can use the formula BMI = kg/m2 where kg is a person’s weight in kilograms and m2 is their height in meters squared. Here is a complete C program code to calculate BMI in float value with 2 decimal places.

/* C Program to print BMI based on formula */
#include <stdio.h>
int main()
{
    float wt,ht,bmi;
    printf("\n\t Enter Weight In Kg\t");
    scanf("%f",&wt);
    printf("\n\t Enter height In Meters\t");
    scanf("%f",&ht);
    //Calculate BMI 
    bmi = wt / (ht*ht);
    if (bmi <= 18.5)
    {
        printf ("BMI Report is : %.2f -> Underweight", bmi);
    }else if(bmi <= 24.9)
    {
        printf("BMI Report is :  %.2f -> Normal",bmi);
    }
    else if(bmi >= 25 && bmi <= 29.9)
    {
        printf("BMI Report is : %.2f -> Overweight",bmi);
    }
else
   printf("BMI Report is : %.2f -> Obese",bmi);   
    return 0;
}

Output after after calculating body mass index using c program is below

Enter Weight In Kg	70
Enter height In Meters	2
BMI Report is : 17.50 -> Underweight

Programs to print different Square, rectangle in C Language

Print hollow square using for loop for user input length.

Steps to build this program
1) Accept length from user
2) First for loop to iterate pointer row wise
3) second inner for loop for to iterate pointer column wise
4) IF statement to compare first and last row for printing star
5) Else condition to print star for first and last column of every row.

Here is the complete code

/* C Program to print Hollow SQUARE  */

#include <stdio.h>

int main()
{
    int i, j, len;

    // Get Length of square from user 
    printf("Enter length of square: ");
    scanf("%d", &len);
    printf("\n");
 // This for loop to change pointer row wise
    for(i=1; i<=len; i++)
    {
        //This for loop to print *
        for(j=1; j<=len; j++)
        {
            //condtion to print all star for first and last line
            //Also to print first and last star of evey line
            if (i==1 || i == len || j ==1 || j == len)
            {
                printf(" *");
            } 
            //this else part to print blank for rows between 1 and n
            else
            {
                printf("  ");
            }
        }
        printf("\n");
    }
    return 0;
}

Output

Enter length of square: 8

 * * * * * * * *
 *             *
 *             *
 *             *
 *             *
 *             *
 *             *
 * * * * * * * *

Printing spaces can be handled. By providing extra spaces in print statement we can print suitable hollow square


Print Hollow square using While Loop

/* C Program to print Hollow SQUARE   */

#include <stdio.h>

int main()
{
    int i=1, j=1, len;

    // Get Length of square from user 
    printf("Enter length of square: ");
    scanf("%d", &len);
    printf("\n");
 // This while loop to change pointer row wise
    while (i<=len)
    {
        //This while loop to print star column wise
        j=1;
        while (j<=len)
        {
            //condtion to print all star for first and last line
            //Also to print first and last star of evey line
            if (i==1 || i == len || j == 1 || j == len)
            {
                printf("*");
            } 
            //this else part to print blank for rows between 1 and n
            else
            {
                printf(" ");
            }
            j++;
        }
        printf("\n");
        i++; 
    }
   
    return 0;
}

Output

Enter length of square: 5
*****
*   *
*   *
*   *
*****

Program to print rectangle by accepting width and breadth.

Print hollow square using while loop for user input width and breadth.

Steps to build this program
1) Accept width and breadth from user
2) First while loop to iterate pointer breadth (row) wise
3) Second inner while loop for to iterate pointer width (column) wise
4) IF statement to compare first and last row for printing star
5) Else condition to print star for first and last column of every row.

Here is the complete code

/* C Program to print rectangle based on user input */

#include <stdio.h>

int main()
{
    int i=1, j=1;
    int breadth,width;

    // Get Length of square from user 
    printf("Enter width square: ");
    scanf("%d", &width);
    printf("Enter breadth square: ");
    scanf("%d", &breadth);
    printf("\n");
 // This while loop to change pointer row wise
    while (i<=breadth)
    {
        //This while loop to print star column wise
        j=1;
        while (j<=width)
        {
            //condition to print all star for first and last line
            //Also to print first and last star of evey line
            if (i==1 || i == breadth || j == 1 || j == width)
            {
                printf("*");
            } 
            //this else part to print blank for rows between 1 and n
            else
            {
                printf(" ");
            }
            j++;
        }
        printf("\n");
        i++; 
    }
   
    return 0;
}

Output

Enter width square: 5
Enter breadth square: 3
*****
*   *
*****

In case if we want to change spacing between rows and column then we need to add more spaces in print statement available in while statement.


Program to print solid rectangle or square based on user input.

  1. Accept width and height from user
  2. Write first while loop to print star row wise
  3. Write second inner while loop to print star column wise
/* C Program to print solid rectangle  */
#include <stdio.h>
int main()
{
    int i=1, j=1;
    int breadth,width;

    printf("Enter width square: ");
    scanf("%d", &width);
    printf("Enter breadth square: ");
    scanf("%d", &breadth);
    printf("\n");
 // This while loop to change pointer row wise
    while (i<=breadth)
    {
        //This while loop to print star column wise
        j=1;
        while (j<=width)
        {
            printf ("*");
            j++;
        }
        printf("\n");
        i++; 
    }
   
    return 0;
}

OUTPUT

Enter width square: 6
Enter breadth square: 4
******
******
******
******

Program to identify and print perfect number in C Language

This C Program is for to identify perfect number. Here program is accepting range up to which need to identify perfect number. Perfect number is  a perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. for example 6 where it is divisible for 1, 2, 3 so sum of it is 6. To write this program we have used two for loop along with simple if condition.

// Program to find perfect number
#include <stdio.h>

int main() {
    // Write C code here
    int rangeNo,counterNo,perSum;
  
    printf("\n\tEnter Range to find perfect number \t");
    scanf("%d",&rangeNo);
   //Loop to find divisible numbers 

    for (int i = 1;i<=rangeNo;i++)
    {
        perSum=0;
          for (int j = 1;j<i;j++)
            {
                if (i%j == 0)
                perSum = perSum + j;
             }
            if (perSum == i)
            printf("\n\t %d is perfect number ",i);
    }
    return 0;
}

Output

Enter Range to find perfect number 	10000
6 is perfect number 
28 is perfect number 
496 is perfect number 
8128 is perfect number 

Number divisible by 3 or 5 in C Language

This C Program will accept number accept range from user and then print numbers which are divisible by 3 or 5.

In this program we have accepted range from user. After that using mod operator and for loop we are printing numbers which are divisible by 3 or 5. Compile and Run below program to get the output.

#include <stdio.h>

int main() {
 
    int rangeNo;
    printf("Enter number upto which you need to find number divisible by 3 or 5");
    scanf("%d",&rangeNo);
    //Loop to print number which are divisible by 3
    printf("\n\nThese numbers are divisible by 3\n\n\t\t");
    for (int i = 1;i<=rangeNo;i++)
    {
        
        if (i%3==0)
        printf("\t%d",i);
    }
    printf("\n\nThese numbers are divisible by 5\n\n\t\t");
    for (int i = 1;i<=rangeNo;i++)
    {
        
        if (i%5==0)
        printf("\t%d",i);
    }
    return 0;
}

Output

Enter number upto which you need to find number divisible by 3 or 5300
These numbers are divisible by 3

			3	6	9	12	15	18	21	24	27	30	33	36	39	42	45	48	51	54	
57	60	63	66	69	72	75	78	81	84	87	90	93	96	99	102	105	108	
111	114	117	120	123	126	129	132	135	138	141	144	147	150	153	156	159	162	
165	168	171	174	177	180	183	186	189	192	195	198	201	204	207	210	213	216	
219	222	225	228	231	234	237	240	243	246	249	252	255	258	261	264	267	270	273	276	279	282	285	288	291	294	297	300

These numbers are divisible by 5

			5	10	15	20	25	30	35	40	45	50	55	60	65	70	75	80	85	
90	95	100	105	110	115	120	125	130	135	140	145	150	155	160	165	170	175	
180	185	190	195	200	205	210	215	220	225	230	235	240	245	250	255	260	265	
270	275	280	285	290	295	300