C program to print all the natural numbers from 1 to N can be written in both iterative and recursive methods. In the iterative method, the task can be performed by using for loop, while loop and do-while loop.
Here, all the C programs using different loops have been covered to print the natural numbers. The algorithm, time complexity and pseudocode of the program have also been added.
Page content(s):
Additional content(s):
1. Algorithm to Print First N Natural Numbers
1. Take a natural number n as input.
2. Set another variable i=1
3. Iterate i from 1 to n
4. Print value of i in each iteration
2. Pseudocode to Print First N Natural Numbers
Input : A natural number $n$
Output : All the natural numbers from 1 to $n$
1. Procedure printUptoN($n$):
2.
3.
4.
5.
6.
7. End Procedure
3. Time complexity to Print First N Natural Numbers
Time Complexity: O(n)
Here, $n$ is the no. of natural numbers.
4. C Program & output to Print First N Natural Numbers
/****************************************************************** alphabetacoder.com C program to print all natual numbers from 1 to n using for loop *******************************************************************/ #include <stdio.h> int main() { int n, i; //take input of the number upto which // natural numbers will be printed printf("Enter the upper limit = "); scanf("%d", & n); // iterate from 1 to n and print the number printf("First %d natural numbers are : ", n); for (i = 1; i <= n; i++) printf("%d ", i); return 0; }
Output
Case 1:
Enter the upper limit = 10
First 10 natural numbers are : 1 2 3 4 5 6 7 8 9 10
Case 2:
Enter the upper limit = 5
First 5 natural numbers are : 1 2 3 4 5
/************************************************** alphabetacoder.com C program to print all natual numbers from 1 to n using while loop **************************************************/ #include <stdio.h> int main() { int n, i; //take input of the number upto which // natural numbers will be printed printf("Enter the upper limit = "); scanf("%d", & n); // iterate from 1 to n and print the number printf("First %d natural numbers are : ", n); i = 1; while (i <= n) { printf("%d ", i); i++; } return 0; }
Output
Case 1:
Enter the upper limit = 10
First 10 natural numbers are : 1 2 3 4 5 6 7 8 9 10
Case 2:
Enter the upper limit = 5
First 5 natural numbers are : 1 2 3 4 5
/************************************************** alphabetacoder.com C program to print all natual numbers from 1 to n using do-while loop **************************************************/ #include <stdio.h> int main() { int n, i; //take input of the number upto which // natural numbers will be printed printf("Enter the upper limit = "); scanf("%d", & n); // iterate from 1 to n and print the number printf("First %d natural numbers are : ", n); i = 1; do { printf("%d ", i); i++; } while (i <= n); return 0; }
Output
Case 1:
Enter the upper limit = 10
First 10 natural numbers are : 1 2 3 4 5 6 7 8 9 10
Case 2:
Enter the upper limit = 5
First 5 natural numbers are : 1 2 3 4 5