Print 1 to 10 using while loop in C – Use while loop to print 1 to 10. Initialize a num variable by 1 and set the target variable with 10.
Set condition in the while i.e. num <= target, so, when condition is false the while loop will stop printing the number.
In each iteration, num will be incrementd by 1 and print the number on console.
/*---------------------------------------------------
* Print 1 to 10 using while loop in C - program example
*/
#include<stdio.h>
int main(){
//Initialize starting number
int num = 1;
//Initialize target number
int target = 10;
/*
* loop through using while loop from the
* value of num 1 to 10
* and print the number.
* In every iteration the value of num will be
* incremented by 1.
*/
while (num <= target) {
/*
* Print numbers on console
* use escape sequence \n to print
* Next number in new line
*/
printf("%d \n", num);
//increment the number by 1
++num;
}
return 0;
}
Output:
1
2
3
4
5
6
7
8
9
10