C Program to Print Natural Numbers from 1 to N Loop

In this C program, we will learn about how to print natural numbers from 1 to N using for loop, while loop and do-while loop.

Required Knowledge


C program to print natural numbers from 1 to N using for loop

#include <stdio.h>  
  
int main() {  
    int counter, N;    
    printf("Enter a Positive Number\n");  
    scanf("%d", &N);  
  
    printf("Printing Numbers form 1 to %d\n", N);  

    for(counter = 1; counter <= N; counter++) {  
        printf("%d \n", counter);  
    }
    
    return 0;  
} 

Output
Enter a Positive Number
10
Printing Numbers form 1 to 10
1
2
3
4
5
6
7
8
9
10

C program to print numbers from 1 to N using while loop

#include <stdio.h>  
  
int main() {  
    int counter, N;   
    printf("Enter a Positive Number\n");  
    scanf("%d", &N);  
  
    printf("Printing Numbers form 1 to %d\n", N);  

    counter = 1;
    while(counter <= N) {  
        printf("%d \n", counter);  
        counter++;
    }
    return 0;  
} 

Output
Enter a Positive Number
7
Printing Numbers form 1 to 7
1
2
3
4
5
6
7

C program to print numbers from 1 to N using do while loop

#include <stdio.h>  
  
int main() {  
    int counter, N;  
  
    printf("Enter a Positive Number\n");  
    scanf("%d", &N);  
  
    printf("Printing Numbers form 1 to %d\n", N);  
 
    counter = 1;
    do {  
        printf("%d \n", counter);  
        counter++;
    } while(counter <= N);
    
    return 0;  
} 

Output
Enter a Positive Number
5
Printing Numbers form 1 to 5
1
2
3
4
5

Related Topics
C program to print even numbers between 1 to N using for and while loop
C program to print odd numbers between 1 to N using for and while loopr
C program to find sum of all even numbers between 1 to N using for loop
C program to find sum of all odd numbers between 1 to N using for loop
C program to print all prime numbers between 1 to N using for loop
C program to print all prime factors of a number
C program to find perfect numbers between 1 to N using for loop
C program to find product of digits of a number using while loop
C program to print multiplication table of a number
List of all C programs