For Loop in C Program

For Loop in C Language

In this article, I am going to discuss the For loop in C Language with Examples. Please read our previous articles, where we discussed Do While loop in C with Examples. At the end of this article, you will understand what is for loop and when and how to use for loop in C program with examples.

For Loop in C:

A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. The for loop is used to iterate the statements or a part of the program several times. It is frequently used to traverse the data structures like the array and linked list.

For Loop Control flow in C

For loop contains 3 parts, such as

  1. Initialization
  2. Condition
  3. Iteration (increment or decrement statement)
Syntax to use for loop in C:

Syntax to use foe loop in C

When we are working with for loop always execution process will start from the initialization block. After the initialization block, the control will pass to the condition block. If the condition is evaluated as true then control will pass to the statement block.

After execution of the statement block, the control will pass to iteration block, from iteration it will pass back to condition. Always repetition will happen beginning condition, statement block, and iteration only. Initialization block will be executed only once when we are entering into the loop first time.

When we are working with for loop everything is optional but mandatory to place 2 semicolons (;;). While we are working with for loop if the condition part is not given then it will repeat infinite times because the condition part will replace it as non-zero. So it is always true as like for(; 1; )

Whenever we are working with for loop it repeats in an anticlockwise direction. In for loop also the pre-checking process will occur i.e. before the execution of the statement block (body of the for loop), condition part will be executed

Program: WAP to print the number from 1 to 10 using for loop
#include <stdio.h>
int main()
{
    int i;
    for (i = 1; i <= 10; i++)
    {
        printf("%d ", i);    
    }
}

Output: 1 2 3 4 5 6 7 8 9 10

Note: In implementation when we need to create more than one initialization, more than one iteration part then use the comma as a separator for every expression. Whenever the body is not specified for the loop then automatically scope will be terminated to next (;) i.e. under the condition only 1 statement will be placed.

Example: WAP to enter a number and check whether that no is the perfect number or not?

If the sum of all factors is equal to that number then it is called a perfect number.

#include <stdio.h>
int main()
{
    int n, i, sum = 0;
    printf("\nenter a number ");
    scanf("%d", &n);
    for(i = 1; i<= n/2; i++)
    {
        if(n % i == 0)
        sum = sum + i;
    }
    if (sum == n && n != 0)
        printf("it is a perfect number");
    else
        printf("it is not a perfect number");
    return 0;
}

Output:

Example: WAP to check whether a number is Armstrong no or not.

If the sum of all individual cube values is equal to that number then it is called Armstrong number.

#include <stdio.h>
int main()
{
    int n, rem, temp, sum = 0;
    printf("\nEnter a number : ");
    scanf("%d", &n);
    for(temp = n; temp != 0;)
    {
        rem = temp % 10;
        sum = sum + (rem* rem* rem);
        temp = temp /10;
    }
    if (sum == n && n != 0)
        printf("It is an Armstrong number");
    else
        printf("It is not an Armstrong number");
    return 0;
}

Output:

Example: WAP to enter a number and check whether it is a prime number or not?

The number which is divisible by 1 and itself is called a prime number.

#include <stdio.h>
int main()
{
    int n, i;
    printf("\nEnter a number : ");
    scanf("%d", &n);
    for(i = 2; i < n; i++)
    {
        if(n % i == 0)
        break;
    }
    if (i == n && n >= 2)
        printf("It is a prime number");
    else
        printf("It is not a prime number");
    return 0;
}

Output:

Example: WAP to print the Fibonacci series up to a given number.
#include <stdio.h>
int main()
{
    int n, n1 = 0, n2 = 1, temp;
    printf("\nEnter a number : ");
    scanf("%d", &n);
    if(n >= 1)
    {
        printf("%d %d ", n1, n2);
        temp = n1 + n2;
        
        for(; temp <= n;)
        {
                printf("%d ", temp);
                n1 = n2;
                n2 = temp;
                temp = n1+n2;
        }
    }    
    else
        printf("please enter a number greater than zero");
}

Output:

Nested for Loop in C:

In nested for loop one or more statements can be included in the body of the loop. In nested for loop, the number of iterations will be equal to the number of iterations in the outer loop multiplied by the number of iterations in the inner loop. The syntax to use nested for loop is given below.

Nested For loop in C

Program to understand nested for loop in C:
#include <stdio.h>
int main()
{
 int i; 	//for outer loop counter
 int j; 	//for inner loop counter
 for( i=1; i<=5; i++)
 {
  for( j=1; j<=10; j++)
  {
   printf("%d ",j);
  }
  printf("\n");
 }
 
 return 0;
}

Output:

C Nested For Loop Example

In the next article, I am going to discuss Break Statements in C Program with examples. Here, in this article, I try to explain For Loop in C Program with examples. I hope you enjoy this For Loop in C Language article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *