Program example to print 1 to 10 using for loop in C. Create and initialize a num variable by 1 and set the target variable with 10 as we want to print numbers up to 10.
Iterate the for loop from starting num i.e. 1 to target value 10. In each iteration, num variable will be incremented by 1 and print the number on console.
When condition i.e. “num <= target” in for loop become false, means target value is greater than 10, then the for loop will stop.
Example to print 1 to 10 using for loop in C
/*---------------------------------------------------
* Print 1 to 10 using for loop in C - program example
*/
#include<stdio.h>
int main(){
/* Initialize starting number */
int num = 1;
/* Initialize target number */
int target = 10;
/*
* loop through the for loop from the value of
* num 1 to target value 10
* and print the number.
* In every iteration the value of num will be
* incremented by 1.
*/
for (num = 1; num <= target; ++num) {
/*
* Print numbers on console
* use escape sequence \n to print
* Next number in new line
*/
printf("%d\n", num);
}
return 0;
}
Output:
1
2
3
4
5
6
7
8
9
10