C Program to Print Natural Numbers from 1 to N using For, While and Do While Loop

  • Write a C program to print natural numbers from 1 to N using for loop.
  • Write a C program to print numbers from 1 to 10 using while loop.
  • Write a C program to print numbers from 1 to 100 using 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;  
    /* 
     * Take a positive number as input form user 
     */  
    printf("Enter a Positive Number\n");  
    scanf("%d", &N);  
  
    printf("Printing Numbers form 1 to %d\n", N);  
    /* 
     * Initialize the value of counter to 1 and keep on 
     * incrementing it's value in every iteration till
     * counter <= 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;  
    /* 
     * Take a positive number as input form user 
     */  
    printf("Enter a Positive Number\n");  
    scanf("%d", &N);  
  
    printf("Printing Numbers form 1 to %d\n", N);  
    /* 
     * Initialize the value of counter to 1 and keep on 
     * incrementing it's value inside while loop body 
     * in every iteration 
     */ 
    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;  
    /* 
     * Take a positive number as input form user 
     */  
    printf("Enter a Positive Number\n");  
    scanf("%d", &N);  
  
    printf("Printing Numbers form 1 to %d\n", N);  
    /* 
     * Initialize the value of counter to 1 and keep on 
     * incrementing it's value inside do-while loop body 
     * in every iteration 
     */ 
    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