Coding Practice

Write a C program to print all natural numbers in reverse (from n to 1). - Using while loop

Print all natural numbers in reverse (from n to 1). - using while loop
Sample Output
Enter a number: 6
6
5
4
3
2
1

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

int main()
{
    int num, n;

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

    n = num;
    while(n >= 1)
    {
        printf("%d\n", n);
        n--;
    }
    return 0;
}
Sample Output
Enter a number: 4
4
3
2
1

Process returned 0 (0x0)   execution time : 280.473 s
Press any key to continue.

No comments:

Post a Comment

Change Theme
X