C program to print all natural numbers from 1 to n; Through this tutorial, we will learn how to c program to print all natural numbers from 1 to n (10, 100, 500, 1000, etc) using for loop, while loop and recursion function.
Algorithm to print all natural numbers from 1 to n
Use the following algorithm to write a c program to print all natural numbers from 1 to n (10, 100, 500, 1000, etc); as follows:
- Step 1: Start
- Step 2: Assign i=1
- Step 3: Read a number, num
- Step 4: Repeat step 5&6 until i=num reach
- Step 5: Print i
- Step 6: Compute i=i+1
- Step 7: Stop
Programs to Print Natural Numbers from 1 to N in C
- C Program to Print Natural Numbers from 1 to N using While Loop
- C Program to Print Natural Numbers from 1 to N using For Loop
- C Program to Print Natural Numbers from 1 to N using Recursion
C Program to Print Natural Numbers from 1 to N using While Loop
/* C Program to Print Natural Numbers from 1 to N using While Loop */
#include<stdio.h>
int main()
{
int Number, i = 1;
printf("\n Please Enter any Integer Value :- ");
scanf("%d", &Number);
printf("\n List of Natural Numbers from 1 to %d are \n", Number);
while(i <= Number)
{
printf(" %d \t", i);
i++;
}
return 0;
}
The output of the above c program; as follows:
Please Enter any Integer Value :- 10 List of Natural Numbers from 1 to 10 are 1 2 3 4 5 6 7 8 9 10
C Program to Print Natural Numbers from 1 to N using For Loop
/* C Program to Print Natural Numbers from 1 to N using For Loop */
#include<stdio.h>
int main()
{
int Number, i;
printf("\n Please Enter any Integer Value : ");
scanf("%d", &Number);
printf("\n List of Natural Numbers from 1 to %d are \n", Number);
for(i = 1; i <= Number; i++)
{
printf(" %d \t", i);
}
return 0;
}
The output of the above c program; as follows:
Please Enter any Integer Value :- 5 List of Natural Numbers from 1 to 5 are 1 2 3 4 5
C Program to Print Natural Numbers from 1 to N using Recursion
#include<stdio.h>
void display(int);
int main()
{
int limit;
printf("Please Enter any Integer Value : ");
scanf("%d", &limit);
printf("\nNatural Numbers from 1 To %d are:", limit);
display(limit);
return 0;
}
void display(int num)
{
if(num)
display(num-1);
else
return;
printf(" %d ", num);
}
The output of the above c program; as follows:
Please Enter any Integer Value : 10 Natural Numbers from 1 To 10 are: 1 2 3 4 5 6 7 8 9 10