C Program to print natural numbers in reverse

In this blog post, we learn how to write a C program to print natural numbers from 1 to n.  We will take the help of iterative statements like for, while or do-while loop to print natural numbers in reverse.

What are Natural numbers?

Natural numbers are the positive integers or non-negative integers which starts from 1 and ends at infinity, such as

1,2,3,4,5,6,7,8,9,10,……,∞

 

 

C Program to Print Natural Numbers in reverse using For Loop

In the below program, we will ask the user to enter the value of ‘n’. After entering the value of ‘n’ we will print natural numbers in reverse with the help of for loop.

#include<stdio.h>

int main()
{
    int num, i;
    printf("Please Enter any Integer number = ");
    scanf("%d", &num);
    if(num >0)
    {
        printf("List of Natural Numbers from %d to 1 are\n", num);
        for(i = num; i >= 1; i--)
        {
            printf("%d ", i);
        }
    }
    else
    {
        printf("Enter Valid number\n");
    }
    return 0;
}

Output:

Please Enter any Integer number = 10
List of Natural Numbers from 10 to 1 are
10 9 8 7 6 5 4 3 2 1

 

C Program to print natural numbers in reverse using while loop

In the below program, we will ask the user to enter the value of ‘n’. After entering the value of ‘n’ we will print natural numbers in reverse order with the help of for loop. with the help of a while loop.

#include<stdio.h>

int main()
{
    int num, i = 0;

    printf("Please Enter any Integer number = ");
    scanf("%d", &num);

    if(num >0)
    {
        i = num;
        printf("List of Natural Numbers from %d to 1 are \n", num);
        while(i >= 1)
        {
            printf("%d ", i--);
        }
    }
    else
    {
        printf("Enter Valid number\n");
    }
    return 0;
}

Output:

Please Enter any Integer number = 10
List of Natural Numbers from 10 to 1 are
10 9 8 7 6 5 4 3 2 1

 

C program to print natural numbers in reverse within a range

The mentioned C program prints natural numbers in reverse order in a given range. The minimum and maximum value of the range ask by users.

#include<stdio.h>
int main()
{
    int rangeMinValue,rangeMaxValue, i;

    printf("Please Enter the rangeMinValue of natural number = ");
    scanf("%d",&rangeMinValue);

    printf("Please Enter the rangeMaxValue of natural number = ");
    scanf("%d",&rangeMaxValue);

    if((rangeMinValue > 0) && (rangeMaxValue > 0) && (rangeMaxValue > rangeMinValue ))
    {
        printf("List of Natural Numbers from %d to %d are \n",rangeMaxValue,rangeMinValue);
        for(i = rangeMaxValue ; i >= rangeMinValue; i--)
        {
            printf("%d ", i);
        }
    }
    else
    {
        printf("Enter Valid numbers\n");
    }
    return 0;
}

Output:

Please Enter the rangeMinValue of natural number = 1
Please Enter the rangeMaxValue of natural number = 10
List of Natural Numbers from 10 to 1 are
10 9 8 7 6 5 4 3 2 1

Leave a Reply

Your email address will not be published. Required fields are marked *