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

## 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 loops to print natural numbers. Below is the step by step descriptive logic to print natural numbers from 1 to n.

- Read the upper limit to print natural number from user. Store it in some variable say N.
- Initialize a for loop starting from 1, goes till N with 1 increment.
The loop structure should be like for(i=1; i<=N; i++). At this point you might be thinking of various things such as.
Why starting from 1? Because we are printing natural numbers from 1.

Why going till N? Because we are printing natural numbers upto N.

Why incrementing the loop counter by 1? Because difference between two natural numbers is 1.

- Inside the loop body print the value of i. Now why printing the value of i? Because we need to print the natural numbers from 1 to N and from the loop structure it is clear that i will iterate from 1 to N.

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 the value of n */ 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; }

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 the above logic and program. You can easily find a way to print natural numbers in range (x to y). If not here is a hint.

- Input starting value from user, store it in some variable say start.
- Input ending value from user, store it in some another variable say end.
- Now, the most important thing to do. Change the above program loop structure. Initialize the loop from start that goes till end. The loop structure should be 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 value */ 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; }

Before you move on to the next exercise or program. Check out other approaches to print natural numbers.

Enter start value: 10 Enter end value: 15 Natural numbers from 10 to 15 : 10 11 12 13 14 15

Happy coding ;)

### You may also like

- 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.
- C program to enter any number and calculate its factorial.
- C program to enter any number and find its first and last digit.

< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)