In this tutorial, you will learn how to write a program to print all natural numbers from 1 to n using a while loop in C. Here, we will go through various examples, logic, algorithms and program explanation for your better understanding. Basically we will focus on writing the program using while loop.
Required Knowledge
Problem Statement
You have to write a C program that takes a positive integer ‘n’ as input. After taking an input it should print all the natural numbers from 1 to n using a while loop. The program should provide a solution to print all the natural numbers up to a given limit using a while loop. Below is an example:
Examples
Input:
Enter the value of n: 7
Output:
1 2 3 4 5 6 7
In the above example, you can see that the user has given input 7. Our program has printed output 1 2 3 4 5 6 7. Here all these numbers are natural numbers.
Algorithm
- Start the program.
- Declare the variables:
n
to store the input value andnum
to represent the current number. - Read the value of
n
from the user. - Initialize
num
to 1. - Print the header message: “Natural numbers from 1 to n:”
- Enter the while loop with the condition
num <= n
.- Print the value of
num
. - Increment
num
by 1.
- Print the value of
- Exit the while loop.
- Print a new line to separate the output.
- End the program.
Logic We have used to Solve this problem
- Read the value of ‘n’ as the upper limit of the natural numbers.
- Initialize a counter variable to 1.
- Use a while loop that runs as long as the counter is less than or equal to ‘n’.
- Inside the loop, print the value of the counter.
- Increment the counter by 1 in each iteration.
- Continue the loop until the counter reaches the value of ‘n’.
- Finish the program.
Program to print all natural numbers from 1 to n
#include <stdio.h>
int main() {
int n, counter = 1;
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Natural numbers from 1 to n: ");
while (counter <= n) {
printf("%d ", counter);
counter++;
}
printf("\n");
return 0;
}
Output
Enter the value of n: 12
Natural numbers from 1 to n: 1 2 3 4 5 6 7 8 9 10 11 12
Program Explanation
- The program takes input for the value of n from the user.
- It initializes a variable
counter
to 1, which represents the current number being printed. - The program enters a while loop with the condition
counter <= n
to iterate through numbers from 1 to n. - Inside the while loop, it prints the value of
<code>counter
on the screen. - It then increments the value of
<code>counter
by 1 to move on to the next number. - The loop continues until
<code>counter
exceeds the value of n. - Once the while loop is complete, the program ends.
This program explanation provides a step-by-step breakdown of how the program works to print all natural numbers from 1 to n using a while loop.
Conclusion
The C program successfully prints all the natural numbers from 1 to the given input n
using a while loop. By iterating through the numbers and incrementing the counter within the loop, each natural number is printed on a single line.