C Program to Print Natural Numbers from 1 to N In Reverse Order using While loop

Lets write a simple C program to print natural numbers from 1 to N in reverse order, using while loop.

Related Read:
while loop in C programming
C Program to Print Natural Numbers from 1 to N using While loop

Source Code: C Program to Print Natural Numbers from 1 to N In Reverse Order using While loop

 
#include < stdio.h >

int main()
{
    int num;

    printf("Enter a positive number\n");
    scanf("%d", &num);

    printf("\nNatural numbers from 1 to %d are:\n", num);

    while(num)
    {
        printf("%d  ", num);
        num--;
    }

    printf("\n");

    return 0;
}

Output 1:
Enter a positive number
10

Natural numbers from 1 to 10 are:
10 9 8 7 6 5 4 3 2 1

Output 2:
Enter a positive number
14

Natural numbers from 1 to 14 are:
14 13 12 11 10 9 8 7 6 5 4 3 2 1

C Program to Print Natural Numbers from 1 to N In Reverse Order using While loop


[youtube https://www.youtube.com/watch?v=RDh7zjoLYNg]

YouTube Link: https://www.youtube.com/watch?v=RDh7zjoLYNg [Watch the Video In Full Screen.]


Logic To Print Natural Numbers from 1 to N In Reverse Order using While loop

We ask the user to enter a positive number, and store it inside a variable num. We iterate through the loop until the number is equal to zero. For example, if user enters num = 5, we iterate the loop 5 times. Inside the while loop we keep decrementing the value of variable num. Once the value of num becomes 0, we exit the loop. For each iteration we print the value of num.

This way we printout all the natural numbers from 1 to N, in reverse order.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program to Print Natural Numbers from 1 to N using While loop

Lets write a simple C program to print natural numbers from 1 to N, using while loop.

Related Read:
while loop in C programming

Source Code: C Program to Print Natural Numbers from 1 to N using While loop

 
#include < stdio.h >

int main()
{
    int num, count = 1;

    printf("Enter a positive number\n");
    scanf("%d", &num);

    printf("\nNatural numbers from %d to %d:\n", count, num);

    while(count <= num)
    {
        printf("%d  ", count);
        count++;
    }

    printf("\n");

    return 0;
}

Output 1:
Enter a positive number
10

Natural numbers from 1 to 10:
1 2 3 4 5 6 7 8 9 10

Output 2:
Enter a positive number
14

Natural numbers from 1 to 14:
1 2 3 4 5 6 7 8 9 10 11 12 13 14

C Program to Print Natural Numbers from 1 to N using While loop


[youtube https://www.youtube.com/watch?v=smJXa0WDOPU]

YouTube Link: https://www.youtube.com/watch?v=smJXa0WDOPU [Watch the Video In Full Screen.]


Logic To Print Natural Numbers from 1 to N using While loop

We start by assigning 1 to variable count. Now we ask the user to enter a positive number. Now while loop keeps executing until value of count is less than or equal to user entered value. Inside while loop we printout the value of count and then increment the value of count by one for each iteration of while loop.

This way we printout all the natural numbers from 1 to N.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert