1.
Introduction
2.
Code
2.1.
Method 1: Using For Loop
3.
Method 2: Using While Loop
4.
Method 3: Using Do-while Loop
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

# Natural Numbers Program using C

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

## Introduction

Natural Numbers are non-negative whole numbers starting from 1, i.e. a set of positive integers {1,2,3,....................... so on}. Natural numbers are used for counting or ordering {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11â€¦}

To print all the natural numbers from 1 to n, we will use loop statements of C language to carry out the program.

## Code

This program allows the user to enter an integer. Using For loop, we will print the list of natural numbers from 1 to the entered value.

### Method 1: Using For Loop

In this method, we will take a for loop and iterate a variable i from 1 to the user entered limit n by incrementing by 1 through every loop. Then, inside the loop we will print each number(i) up to n.

``````#include<stdio.h>
void main() {
int i,n=14;
for(i=1;i<=n;i++){
printf("%d ", i);
}
printf("\n");
}``````

Output

``1 2 3 4 5 6 7 8 9 10 11 12 13 14``

Also see, Short int in C Programming

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## Method 2: Using While Loop

In this method, we will take a while loop and iterate a variable i from 1 to the user entered limit n by incrementing by 1 through every loop. Then, inside the loop we will print each number(i) up to n.

``````#include<stdio.h>
void main() {
int n=7;
int i=1;
while(i<=n){
printf("%d ", i);
i++;
}
printf("\n");
}``````

Output

``1 2 3 4 5 6 7``

You can also read about dynamic array in c, and Tribonacci Series

## Method 3: Using Do-while Loop

In this method, we will take a do while loop and iterate a variable i from 1 to the user entered limit n by incrementing by 1 through every loop. Then, inside the loop we will print each number(i) up to n.

``````#include<stdio.h>
void main() {
int n=17;
int i=1;
do{
printf("%d ", i);
i++;
}while(i<=n);
printf("\n");
}``````

Output

``1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17``

You can practice by yourself with the help of an online C compiler.

Time Complexity: O(N) for all the codes because we have used only one loop for printing the numbers

Space Complexity: O(1)
Check out this problem - First Missing Positive

## FAQs

1. What is a natural number in C?
Natural Numbers are non-negative whole numbers starting from 1, i.e. a set of positive integers {1,2,3,....................... so on}. Natural numbers are used for counting or ordering.

2. How can we print natural numbers in C?
The user enters a number n. We take a loop from 1 to n, and print all the numbers one by one.

3. Which loops can be used to print natural numbers in C?
All the looping statements like for, while and do-while can be used to print all the natural numbers upto n in C.

## Key Takeaways

In this blog, we learnt how to print all natural numbers up to the entered number(n). We hope that this blog has helped you enhance your knowledge, and if you wish to learn more, check out our  Coding Ninjas Blog site and visit our Library. Do upvote our blog to help other ninjas grow.

Happy Learning!

Live masterclass