C program to print all natural numbers from 1 to n

Category: C Program

C program to print all natural numbers from 1 to n using while loop and for loop. How to print natural number from 1 to n using while loop and for loop in C language.

C program to print all natural numbers from 1 to n. - using while loop

#include <stdio.h>

void main()
{
    int n, i = 1;
    printf("Enter value of n: ");
    scanf("%d", &n);
    while(i <= n)
    {
        printf("%d\n", i);
        i++;
    }
}

C program to print all natural numbers from 1 to n. - using for loop

#include <stdio.h>

void main()
{
    int n, i;
    printf("Enter value of n: ");
    scanf("%d", &n);
    for (i = 1; i <= n; i++)
        printf("%d\n", i);
}

Output

Enter value of n: 10
1
2
3
4
5
6
7
8
9
10

Recommended Posts