C Program to Print Natural Numbers From 1 to N

In this program, we will learn how to print natural numbers from 1 to N using C Programming language.

We will use following approaches to print the natural numbers from 1 to N:

  1. Using For Loop
  2. Using While Loop
  3. In a Range

So, without further ado, let’s begin this tutorial.

C Program to Print Natural Numbers From 1 to N Using For Loop

// C Program to Print Natural Numbers From 1 to N
#include <stdio.h>
int main(){
    int num;
    
    // Asking for input
    printf("Enter a number: ");
    scanf("%d", &num);
    
    printf("Natural numbers from 1 to %d are: \n", num);
    for (int i = 1; i <= num; i++){
        printf("%d \n", i);
    }
    return 0;
}

Output

Enter a number: 15
Natural numbers from 1 to 15 are: 
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 

How Does This Program Work ?

    int num;

In the above program, we have declared one integer data type variable named num.

    // Asking for input
    printf("Enter a number: ");
    scanf("%d", &num);

Now, the user is asked to enter a positive number. This number gets stored in the num named variable.

    printf("Natural numbers from 1 to %d are: \n", num);
    for (int i = 1; i <= num; i++){
        printf("%d \n", i);
    }

Then, we print all numbers lying between 1 to num using a for loop.

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 i = 1, num;
    
    // Asking for input
    printf("Enter a positive number: ");
    scanf("%d", &num);
    
    printf("Natural numbers from 1 to %d are: \n");
    while (i <= num){
        printf("%d \n", i);
        i++;
    }
    return 0;
}

Output

Enter a positive number: 5
Natural numbers from 1 to 0 are: 
1 
2 
3 
4 
5 

C Program to Print Natural Numbers in a Range

// C Program to Print Natural Numbers in a Range
#include <stdio.h>
int main(){
    int i, min, max;
    
    // Asking for input
    printf("Enter the minimum value: ");
    scanf("%d", &min);
    printf("Enter the maximum value: ");
    scanf("%d", &max);
    
    printf("Natural numbers lying in range %d to %d are: \n", min, max);
    for (i = min; i <= max; i++){
        printf("%d \n", i);
    }
    return 0;
}

Output

Enter the minimum value: 63
Enter the maximum value: 70
Natural numbers lying in range 63 to 70 are: 
63 
64 
65 
66 
67 
68 
69 
70 

Conclusion

I hope after going through this post, you understand how to print natural numbers from 1 to N using C Programming language.

If you have any doubt regarding the program, then contact us in the comment section. We will be delighted to assist you.

Also Read:

Leave a Comment

Your email address will not be published.