In this C program, we will learn about how to print natural numbers from 1 to N using for loop, while loop and do-while loop.
Required Knowledge
C program to print natural numbers from 1 to N using for loop
#include <stdio.h>
int main() {
int counter, N;
printf("Enter a Positive Number\n");
scanf("%d", &N);
printf("Printing Numbers form 1 to %d\n", N);
for(counter = 1; counter <= N; counter++) {
printf("%d \n", counter);
}
return 0;
}
Output
Enter a Positive Number 10 Printing Numbers form 1 to 10 1 2 3 4 5 6 7 8 9 10
C program to print numbers from 1 to N using while loop
#include <stdio.h>
int main() {
int counter, N;
printf("Enter a Positive Number\n");
scanf("%d", &N);
printf("Printing Numbers form 1 to %d\n", N);
counter = 1;
while(counter <= N) {
printf("%d \n", counter);
counter++;
}
return 0;
}
Output
Enter a Positive Number 7 Printing Numbers form 1 to 7 1 2 3 4 5 6 7
C program to print numbers from 1 to N using do while loop
#include <stdio.h>
int main() {
int counter, N;
printf("Enter a Positive Number\n");
scanf("%d", &N);
printf("Printing Numbers form 1 to %d\n", N);
counter = 1;
do {
printf("%d \n", counter);
counter++;
} while(counter <= N);
return 0;
}
Output
Enter a Positive Number 5 Printing Numbers form 1 to 5 1 2 3 4 5
Related Topics