C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Loop Programs in C

Print first N natural numbers

Introduction

Natural numbers are positive integers starting from 1 (i.e., 1, 2, 3, 4, …).
This program takes an integer N from the user and prints all numbers from 1 to N.

We’ll use a for loop for sequential printing.

C Program: Print first N natural numbers

NOTE: The Program is written using for, while and do..while loop.
Method 1 is for loop, Method 2 is while loop and Method 3 is do..while loop

Method 1: Using for loop

C

#include <stdio.h>

 

int main() {

    int N, i;

 

    // Input

    printf("Enter the value of N: ");

    scanf("%d", &N);

 

    printf("First %d natural numbers are:\n", N);

 

    // Loop from 1 to N

    for (i = 1; i <= N; i++) {

        printf("%d ", i);

    }

 

    printf("\n");

    return 0;

}

Output

 
OUTPUT 1 :
Enter the value of N: 10
First 10 natural numbers are:
1 2 3 4 5 6 7 8 9 10

OUTPUT 2 :
Enter the value of N: 5
First 5 natural numbers are:
1 2 3 4 5

Explanation

  1. User enters a number N.
  2. A for loop starts at 1 and runs until N.
  3. Each number is printed in sequence.
  4. The loop stops when i becomes greater than N.

 

C Program: Print first N natural numbers

Method 2: Using while loop

C

#include <stdio.h>

 

int main() {

    int N, i = 1;

 

    // Input

    printf("Enter the value of N: ");

    scanf("%d", &N);

 

    printf("First %d natural numbers are:\n", N);

 

    // While loop from 1 to N

    while (i <= N) {

        printf("%d ", i);

        i++;

    }

 

    printf("\n");

    return 0;

}

Output

 
OUTPUT 1 :
Enter the value of N: 7
First 7 natural numbers are:
1 2 3 4 5 6 7

OUTPUT 2 :
Enter the value of N: 3
First 3 natural numbers are:
1 2 3

Explanation

  1. Initialize i to 1.
  2. Check the condition i <= N.
  3. If true, print i and then increment i by 1.
  4. The loop continues until i becomes greater than N.

C Program: Print first N natural numbers

Method 3: Using do..while loop

A do-while loop is similar to a while loop, except it executes the loop body at least once, even if the condition is false.
This is useful when you want to ensure the statements inside the loop run at least one time.

C

#include <stdio.h>

 

int main() {

    int N, i = 1;

 

    // Input

    printf("Enter the value of N: ");

    scanf("%d", &N);

 

    printf("First %d natural numbers are:\n", N);

 

    // Do-while loop

    do {

        printf("%d ", i);

        i++;

    } while (i <= N);

 

    printf("\n");

    return 0;

}

Output

 
OUTPUT 1 :
Enter the value of N: 5
First 5 natural numbers are:
1 2 3 4 5

OUTPUT 2 :
Enter the value of N: 1
First 1 natural numbers are:
1

Explanation

  1. Initialize i = 1.
  2. The do block executes first, printing the value of i.
  3. i is then incremented by 1.
  4. The while (i <= N) checks the condition at the end of the loop.
  5. If the condition is true, the loop repeats; otherwise, it stops.