Write a C program to print all natural numbers from 1 to n using loop. C program to print first n natural numbers using loop. How to print natural numbers in a given range using loop. Logic to print natural numbers using for loop in C program.
Example
Input
Input upper limit: 10
Output
Natural numbers from 1 to 10: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Required knowledge
Basic C programming, Relational operators, For loop
Logic to print natural numbers from 1 to n
There are various ways to print n numbers. For this post I am concentrating on for
loop to print natural numbers.
Step by step descriptive logic to print natural numbers from 1 to n.
- Input upper limit to print natural number from user. Store it in some variable say N.
- Run a
for
loop from 1 to N with 1 increment. The loop structure should be likefor(i=1; i<=N; i++)
. At this point you might be thinking of various things such as.Why starting from 1? Because we need to print natural numbers from 1.
Why going till N? Because we need to print natural numbers up to N.
Why increment loop counter by 1? Because difference between two natural numbers is 1. Therefore if n is one natural number then next natural number is given by n+1.
- Inside the loop body print the value of i. You might think, why print value of i inside loop? Because we need to print natural numbers from 1 to N and from loop structure it is clear that i will iterate from 1 to N. So to print from 1 to N print the value of i.
Let us code our first loop program.
Program to print natural numbers from 1 to n
/**
* C program to print all natural numbers from 1 to n
*/
#include <stdio.h>
int main()
{
int i, n;
/* Input upper limit from user */
printf("Enter any number: ");
scanf("%d", &n);
printf("Natural numbers from 1 to %d : \n", n);
/*
* Start loop counter from 1 (i=1) and go till n (i<=n)
* increment the loop count by 1 to get the next value.
* For each repetition print the value of i.
*/
for(i=1; i<=n; i++)
{
printf("%d\n", i);
}
return 0;
}
Output
Enter any number: 10 Natural numbers from 1 to 10 : 1 2 3 4 5 6 7 8 9 10
Logic to print natural numbers in range
Using above logic you can easily find a way to print natural numbers in range. If not here is a hint.
- Input start limit from user. Store it in some variable say start.
- Input end limit from user. Store it in some another variable say end.
- Now, the most important thing to do. Change the above program loop structure. Initialize loop from start that runs till end. The loop structure should look like
for(i=start; i<=end; i++)
.
Program to print natural numbers in range
/**
* C program to print all natural numbers in range
*/
#include <stdio.h>
int main()
{
int i, start, end;
/* Input start and end limit */
printf("Enter start value: ");
scanf("%d", &start);
printf("Enter end value: ");
scanf("%d", &end);
printf("Natural numbers from %d to %d : \n", start, end);
/*
* Start loop counter from start (i=start) and go till
* end (i<=end), increment the loop count by 1 to get
* the next value. For each repetition print the value of i.
*/
for(i=start; i<=end; i++)
{
printf("%d\n", i);
}
return 0;
}
Take your programming skills a level further. Learn other approaches to solve this problem.
Learn more –
Output
Enter start value: 10 Enter end value: 15 Natural numbers from 10 to 15 : 10 11 12 13 14 15
Happy coding 😉
Recommended posts
- Loop programming exercises index.
- C program to find sum of all natural numbers between 1 to n.
- C program to print all even numbers between 1 to n.
- C program to print all odd numbers between 1 to n.
- C program to find sum of all even numbers between 1 to n.
- C program to find sum of all odd numbers between 1 to n.