Coding Practice

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

Print all natural numbers from 1 to n. - using while loop
Sample Output
Enter a number: 8
The numbers are:
1
2
3
4
5
6
7
8

Process returned 8 (0x8)   execution time : 6.562 s
Press any key to continue.
                
Source Code
#include<stdio.h>

void main()
{
    int num, n;

    printf("Enter a number: ");
    scanf("%d", &num);

    printf("The numbers are:\n");
    n = 1;
    while(n <= num)
    {
        printf("%d\n", n);
        n++;
    }
}
                        
Sample Output
Enter a number: 5
The numbers are:
1
2
3
4
5

Process returned 8 (0x8)   execution time : 6.562 s
Press any key to continue.
                

No comments:

Post a Comment

Change Theme
X