Write a C program to print 1 to 10 numbers using a while loop
Description: Print the numbers from 1 to 10 using a while loop.
Hint:
- Start with a counter of 1, and increase it in each round until it is more than 10.
C program to print 1 to 10 using a while loop
#include <stdio.h>
int main(void) {
int i = 1;
while (i <= 10) {
printf("%d ", i);
i++;
}
printf("\n");
return 0;
}
Output:
Output of the above program is
1 2 3 4 5 6 7 8 9 10
How it works
- The counter
istarts at 1. - The loop runs while
i <= 10, prints the value and then increases it withi++. - When
ibecomes 11, the condition is false and the loop stops.
Read C While Loop.
Learn more about the similar topics:
| Tutorials |
|---|
| C If Else Statement |
| C Else If Ladder and Nested If |
| C Switch Statement |
| C While Loop |
| C Do While Loop |
| C For Loop |
| C Nested Loops |