Print Numbers in Reverse order in C Language

Program-to-print-Numbers-in-Reverse-order-in-C-language

Program Description:

Write a Program to Print Numbers in Reverse Order in C programming language. The program should accept a positive number( num) from the user and display all the numbers from num to 1 in reverse order or Descending order.

Example Input and Output:

Input:

Enter a Number : 31

Output:

Numbers in Reverse Order (31 - 1) : 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1,

Pre-Requisites:

We are going to use the C loops and the modulus operators in this program. Please go through the following tutorials to learn more about them

Program to Print Numbers in Reverse order in C Algorithm:

  1. Take a number from the user and store it in the variable num.
  2. Make sure num is a positive number, If num is a negative number, Please display an error message and stop the program using the return statement.
  3. We are going to use a Loop to display the numbers in Reverse Order ( Descending order ).
  4. Take a Counter variable cnt and make copy the num to cnt. As we need to print from the num to 1.
  5. Start the loop. The loop will run until the cnt value becomes . while (cnt > 0)
  6. At Each Iteration,
    • Print the counter variable i.e cnt
    • Decrement the counter using the Decrement operator i.e cnt--.
  7. The will terminate, Once the cnt value becomes .

📢 This Program is part of the Series of C Loops Practice Programs

Print Numbers in Reverse order (Descending Order) in C Program using While Loop:

Program Output:

Compile and run the program using your favorite compiler, We are using the GCC compiler here.

$ gcc print-reverse-while-loop.c

The above command generates a.out file. Then Run the executable file and provide a number as input.

As you can see from the above output, The program is printing the numbers in Descending order or Reverse order.

Example 2: Print Numbers in Reverse order from 100 to 1 and also try 1000 to 1 in descending order.

print-numbers-in-reverse-order-in-c-program-output
Note: Results are Truncated to fit to screen

Example 3: Re-Run the program by providing a negative number as input.

Print Numbers in Reverse Order in C language using For loop:

We can use the For loop’s Init step to initialize our cnt=num. and the update step to decrement cnt variable ( cnt--).

Program Output:

Compile and run the program. Here is the output.

Exercise: Do while to print the numbers in Reverse order:

As we need to print at least 1 number, We can use the do while also to print the numbers. Try yourself to write this program.

If you need any Hints, Click on the show more button below to see the program.

Program to Print Only Even Numbers from N in Reverse Order in C:

Let’s change the above program to only print the Even numbers in reverse order.

You can check Even or Odd Number Program for more details. We are using the modulus operator ( cnt%2 == 0) to check if the cnt is even at each iteration.

Program Output:

Compile and Run the Program.

Example 1: Print All Even numbers from number 100 to 1 in Reverse Order:

print-all-odd-numbers-in-reverse-order-in-c
Note: Results are Truncated

Here are a few more examples:

Also Note, That program is displaying an error on a negative number.

Program to Print Only Odd Numbers from N in Reverse Order using Loops:

Let’s print all Negative numbers from the N (Given number) up to 1 in Reverse Order using the For loop.

At each iteration, Check if the cnt is an Odd number by using the cnt%2 == 1.

Program Output:

Here is the program output.

Example 1: Print all Odd numbers from 100 to 1 in Reverse Order:

Let’s try a few more examples

Related Programs:

  1. C Loops Programs Index
  2. C Program to Print Prime Numbers between Two numbers
  3. C Program to print First N Prime numbers
  4. C Program to generate Prime numbers up to N
  5. C Program to Calculate Nth Prime Number
  6. C Program Check Prime Number [Mutliple Methods]
  7. C Program to Convert Decimal Number to Binary Number
  8. C Program to Convert Binary Number to Decimal Number
  9. C Program to Convert Octal Number to Decimal Number
  10. C Program to Calculate the Factorial of Number
  11. C Program to Calculate the Power of Number
  12. C Program to Check Number is Power of 2
  13. C Program to find power of number without Loops
  14. C Program to Find all Factors of a Number

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

3 Responses

  1. […] C Program to Print Numbers in Reverse Order […]

  2. […] C Program to Print Numbers in Reverse Order […]

  3. […] C Program to Print Numbers in Reverse Order […]

Leave a Reply