Printing the Numbers from 1 to N in C Language


A for loop is a repetition control structure which allows us to write a loop that is executed a specific number of times. The loop enables us to perform n number of steps together in one line. In for loop, a loop variable is used to control the loop. The variable declaration is done once as if placed just inside the { on the first run. Then the condition is checked, if it is true the body of the loop will execute, if it is false the loop will stop. Assuming the loop continues, the body will execute and finally when the } is reached the increment statement will execute just before the condition is checked again.

The curly braces are optional (you can one line with a semicolon) if the loop contains just one statement. But, it's always recommended to use braces to avoid misunderstandings and bugs. The for loop components are optional. If your business logic contains one of these parts, you can omit the corresponding component from your for loop.

Syntax:
   for( initial ; condition ; increment / decrement)
   {
        // body of loop;
   }

The program is written in C and it demonstrates the use of the for loop

  • The program starts with the inclusion of the header file "stdio.h" which contains the function printf() used in the program.
  • The program declares an integer variable "i" and prompts the user to enter a limit using the scanf() function and stores the value in the variable "n".
  • The program then enters a for loop, which is a control flow statement that is used to iterate through a sequence of statements a specified number of times. The structure of the for loop is such that the initialization, condition, and increment statements are specified in the first line of the for loop.
  • The initialization statement initializes the variable "i" with 1. The condition statement checks if the value of "i" is less than or equal to "n". The increment statement increments the value of "i" by 1.
  • Inside the for loop, the program uses the printf() function to print the value of the variable "i".
  • The for loop continues to execute until the value of "i" becomes greater than "n". Once the condition becomes false, the program exits the for loop and continues to execute the next statement after the loop.
  • Finally, the program returns 0 to indicate successful execution.

In this program, the for loop will print all the numbers from 1 to n (inclusive) that the user entered. So, the program will print 1, 2, 3 ... n numbers.


Source Code

//For Loop
 
#include<stdio.h>
int main()
{
    int i,n;
    printf("\nEnter The Limit : ");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        printf("\n%d",i);
    }
    return 0;
}
 
To download raw file Click Here

Output

Enter The Limit : 20

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C