Programming Examples of for loop in C.



 Programming Examples of for Loop in C


In this article you learn important Programming examples of for Loop in C with its explanation and output, It contains C programs which improves the logic and help in generating ideas to solve C programs related to for Loop. Various types of C programs are discussed related to for Loop below with its complete explanation ans its output. 

Here you also get about What is for loop in C? What is the syntax of for Loop in C? What is the Data flow Diagram of for Loop in C?


 Programming Examples of for loop in C.

for Loop

for Loop is a type of Entry Control Loop. It is a repetition control structure which allow us to write a loop which is executed a fixed number of times. It enables us to perform number of steps together in one block of statements. It makes complex and lengthy programs code into its simplest form. It reduce the length of code and time and also reduces the load of programmer. We use this type of loop when we know the exact number of times a code need to be executed.

for(initialization expr; Condition expr; Update expr )
{
      // statements that we want to execute
}

Data Flow Diagram of for Loop


                      Programming Examples of for loop in C/data flow diagram of for loop in C


 Programming Examples of for loop in C



    Q 1. Write a C program to Print table of entered number by user through keyboard using for loop.



    int main()
    {
        int i=0,num;
        printf("Enter the number to find its table:\t");
        scanf("%d", &num);
        printf("\nTable of %d:\n\n", num);
        for(i=1;i<=10;i++)
        {
            printf("%d * %2d = %d\n", num, i, num*i);
        }
        return 0;
    }
    
    Enter the number to find its table:     11
    
    Table of 11:
    
    11 *  1 = 11
    11 *  2 = 22
    11 *  3 = 33
    11 *  4 = 44
    11 *  5 = 55
    11 *  6 = 66
    11 *  7 = 77
    11 *  8 = 88
    11 *  9 = 99
    11 * 10 = 110


    Q 2. Write a C program to print all natural numbers from 1 to n using for loop. 



    int main()
    {
        int i=1,n;
        printf("Enter the number to print all natural number till that entered number:\n\n");
        scanf("%d",&n);
        printf("\n");
        for(i=1;i<=n;i++)
        {
            printf("%d\t",i);
        }
        return 0;
    }
    
    Enter the number to print all natural number till that entered number:
    
    10
    
    1       2       3       4       5       6       7       8       9       10


    Q 3. Write a C program to Print all alphabets from a to z using for loop.



    int main()
    {
        int i;
        printf("List of alphabet from a to z:\n\n");
        for(i=97;i<=122;i++)
        {
            printf("%c ",i);
        }
        return 0;
    }
    
    List of alphabet from a to z:
    
    a b c d e f g h i j k l m n o p q r s t u v w x y z


    Q 4. Write a C program to Print all even numbers between 1 to 100 using for loop.



    int main()
    {
        int i;
        printf("List of even numbers between 1 to 100:\n\n");
        for(i=1;i<=100;i++)
        {
            if(i%2==0)
                printf(" %d ",i);
        }
        return 0;
    }
    
    List of even numbers between 1 to 100:
    
     2  4  6  8  10  12  14  16  18  20  22  24  26  28  30  32  34  36  38  40  42  44  46  48  50  52  54  56  58  60  62  64  66  68  70  72  74  76  78  80  82  84  86  88  90  92  94  96  98  100




    Q 5. Write a C program to Print all odd numbers between 1 to 100 using for loop.



    int main()
    {
        int i;
        printf("List of odd numbers between 1 to 100:\n\n");
        for(i=1;i<=100;i++)
        {
            if(i%2!=0)
                printf("%4d",i);
        }
        return 0;
    }
    
    List of odd numbers between 1 to 100:
    
       1   3   5   7   9  11  13  15  17  19  21  23  25  27  29  31  33  35  37  39  41  43  45  47  49  51  53  55  57  59  61  63  65  67  69  71  73  75  77  79  81  83  85  87  89  91  93  95  97  99


    Q 6. Write a C program to find sum of all even numbers between 1 to 100 using for loop.



    int main()
    {
        int i,sum=0;
        printf("List of even numbers between 1 to 100:\n");
        for(i=1;i<=100;i++)
        {
            if(i%2==0)
                {
                    printf("%3d ",i);
                    sum=sum+i;
                }
        }
       printf("\n\nSum of even number between 1 to 100 is %d.",sum);
        return 0;
    }
    
    List of even numbers between 1 to 100:
      2   4   6   8  10  12  14  16  18  20  22  24  26  28  30  32  34  36  38  40  42  44  46  48  50  52  54  56  58  60  62  64  66  68  70  72  74  76  78  80  82  84  86  88  90  92  94  96  98 100
    
    Sum of even number between 1 to 100 is 2550.


    Q 7. Write a C program to find sum of all odd numbers between 1 to n using for loop.



    int main()
    {
        int i,sum=0;
        printf("List of odd numbers between 1 to 100:\n\n");
        for(i=1;i<=100;i++)
        {
            if(i%2!=0)
                {
                    printf(" %3d ",i);
                    sum=sum+i;
                }
        }
        printf("\n\nSum of odd number between 1 to 100 is %d.",sum);
        return 0;
    }
    
    List of odd numbers between 1 to 100:
    
       1    3    5    7    9   11   13   15   17   19   21   23   25   27   29   31   33   35   37   39   41   43   45   47   49   51   53   55   57   59   61   63   65   67   69   71   73   75   77   79   81   83   85   87   89   91   93   95   97   99
    
    Sum of odd number between 1 to 100 is 2500.


    Q 8. Write a C program  to calculate  the sum of first n natural numbers using for loop.



    int main()
    {
        int i,n,sum=0;
        printf("Enter the natural number:\n");
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            sum=sum+i;
        }
        printf("\nSum of first %d natural number is %d",n,sum);
        return 0;
    }
    
    Enter the natural number:
    10
    
    Sum of first 10 natural number is 55


    Q 9. Write a C program  to generate all combinations of 1, 2, and 3 using for loop.



    int main()
    {
        int i,j,k;
        printf("List of combinations of 1, 2 and 3:\n");
        for(i=1;i<=3;i++)
        {
            for(j=1;j<=3;j++)
                {
                for(k=1;k<=3;k++)
                    {
                    if(i!=j&&j!=k&k!=i)
                    printf("%d%d%d\t",i,j,k);
                    }
                }
        }
        return 0;
    }
    
    List of combinations of 1, 2 and 3:
    123     132     213     231     312     321


    Q 10. Write a C program  to print all prime number between 1 to 300 using for loop.



    int main()
    {
        int a,b;
        printf("List of prime numbers between 1 to 300:\n");
        printf("%d\t",a=2);
        for(a=1;a<=300;a++)
            {
                for(b=2;b<=a-1;b++)
                    if(a%b==0)
                        break;
                    else if(b==a-1)
                {
                    printf("%d\t",a);
                }
            }
        return 0;
    }
    
    List of prime numbers between 1 to 300:
    2       3       5       7       11      13      17      19
            23      29      31      37      41      43      47
            53      59      61      67      71      73      79
            83      89      97      101     103     107     109
            113     127     131     137     139     149     151
            157     163     167     173     179     181     191
            193     197     199     211     223     227     229
            233     239     241     251     257     263     269
            271     277     281     283     293

    Conclusion:

    In this page you learn 10 different types of  programs based on for loop in C with its explanation of code and output. And also you learn about Definition of for loop in C with its Data Flow Diagram.These are the programs based on for loop in C which boost your solving skills of these types C programming problems.

    If you are interested then you can also read:




    If you find this article helpful then please share!


    Tags: for loop in Cfor loop in C programmingData flow diagram of  for loop in CExamples of for loop in C Programming Examples of for loop in C


    2 comments:

    Powered by Blogger.