Reverse Number Program in C language

Spread the love


Program Description:

In this article, We are going to look at Reverse Number Program in C programming language. The program should accept a number from the user and display the Reverse of the given number. The program only accepts positive values, If the user enters a negative value, It should display the error message. Here is an example Input and Output of the program.

Example Input and Output:

Example 1:


Enter a Positive Number : 12345


Original Number is : 12345

Reversed Number is : 54321

Pre Requisites:

It is good to have familiarity with the Modulus Operator, Division Operator, and Loops (while, for, and do while). Please read the material below to gain the needed understanding.

Reverse Number Program in C Language Explanation:

We can use any of the C Loops to write this program. So we are going to show the program with all loops.

  • Method 1: Reverse Number using while loop
  • Method 2: Reverse Number using for loop
  • Method 3: Reverse Number using do while loop
  • Method 4: Reverse Number using goto statement [Not Recommended – only for demonstration purposes]

Reverse Number Program Algorithm:

  1. We start the program by asking the user for Input. The user needs to provide the a Positive Number. Once we got the Input we save the input value in the num variable.
  2. Firstly, we check if the num is positive using the if(num <= 0) condition. If the number is Negative or Zero, We are going to display the error message and stop the program using return statement.
  3. If the num is Positive Number, We proceed to next steps.
  4. We use two extra variables, temp and rev_num, The temp variable holds the given num. and we perform all our operations on temp. The rev_num initialized with zero and updated during the program execution and once we finished the program, The rev_num contains the Reversed Number
  5. Now, It is time to start the loop. The main logic is to loop until the temp value becomes less than ( Loop Condition temp > 0). At Each Iteration,
    • We are going to take the remainder of the temp using the modulus operator – rem = temp % 10;
    • Then updating the rev_num using the rev_num = (rev_num * 10) + rem ;, Which is going to reverse the number.
    • Then finally, We are dividing the temp to get to the Quotient which is assigned to temp i.e temp = temp / 10; and used in the next iteration.
    • Here is an example Walkthrough, If you’re trying to reverse the number 123,
    • Then at the first Iteration,
      • The rem will be 3 ( i.e 123 % 10 = 3),
      • And rev_num will be ( 0 * 10) + 3 which is 3,
      • The temp changed to temp = temp / 10 which is temp = 123/10 i.e temp = 12.
    • At the Second Iteration,
      • The rem = 12 % 10 which is rem = 2.
      • rev_num will be (3 * 10) + 2, which is 32
      • The temp will change to temp = 12 / 10, which is equal to temp = 1
    • Third Iteration,
      • The rem = 1 % 10 which is rem = 1.
      • rev_num will be (32 * 10) + 1, which is 321
      • The temp will change to temp = 1 / 10, which is equal to temp = 0
    • As the temp reached , temp > 0 so We stop the loop. and our Reverse Number is rev_num which is equal to 321.
  6. Finally, display the results on the console.

📢 This algorithm is going to be the same for all the loops, Only the loop syntax will change.

Method 1: Reverse Number program in C using While loop:

Here is the program using the while loop. We are going to implement the above algorithm and to iterate over the number, we are going to use the while loop

Program Output:

If the user enter’s Negative Number

Method 2: Reverse Number program in C using For loop:

The program logic is very much the same as the while loop, but as it is for loop, we can take advantage of the for loop’s initialization and update options to Init and update our loop control variables.

Here we are going to initialize the temp ( temp = num )at the for loops Init step and update the temp ( temp = temp / 10 ) in for loops update step.

Program Output:

As you can see from the above output, We got the desired output and the program is displaying the error message on Invalid inputs.

Method 3: Reverse Number program in C using do while loop:

The Do while loop executes the loop at least once. We are performing division and modulo division inside the do-while loop body.

As we are checking if the number is positive at line 12, which ensures us the num is going to be a valid positive number. And we are also not allowing the zero. So we will be good.

Program Output:


Method 4: Reverse Number program in C using goto statement:

We can also repeat the block of code using the goto statement, So we are going to use the goto statement to iterate over our temp and create the Reverse Number.

📢 It is only for demonstration purposes, It is always better and easy for us to use the Loops to iterate over the block of code. So use the loops instead of the goto statement to iterate over the code block.

Program Output:


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...

4 Responses

  1. […] C Program to Reverse a Number […]

  2. […] C Program to Reverse a Number […]

  3. […] C Program to Reverse a Number […]

  4. […] accept an input number from the user and reverses it using recursion. We have already looked at the Iterative method to reverse a number in our earlier […]

Leave a Reply