C Program to Reverse an Integer

C program to reverse an integer.


Problem

Problem Statement:

Write a program to reverse an integer using loops.

Example 1:

Input: 
Enter an integer: 987654321

Output:
The reversed number is 12345789.

Example 2:

Input:
Enter an integer: 0

Output:
The reversed number is 0.

Example 3:

Input:
Enter an integer: -5678

Output:
The reversed number is -8765.

Expectations:

You need to write the complete program using the following template as the starter:

#include <stdio.h>

int main() {
    int n;
    
    printf("Enter a number: ");
    scanf("%d", &n);
    
    // Write your code here.

    return 0;
}

Copy and paste the above code in your favorite IDE and start writing your code after the comment “// Write your code here.”

Pro tip:  First, try writing the code on your own. Try every possible thing you can to come up with the solution. If you still can’t figure it out, then check the solution and see what went wrong. This is how you learn. 🙂

Sample Test Cases:

InputOutput
Test Case 1123321
Test Case 210011001
Test Case 3-98-89
Test Case 400

Don’t forget to try the code on your own before diving into the solution.


Solution

The solution to the given problem is as follows:

// Program to reverse an integer
#include <stdio.h>

int main() {
    int n;
    
    printf("Enter a number: ");
    scanf("%d", &n);
    
    // Write your code here
    
    int reversed = 0, remainder;
    
    while (n != 0) {
        remainder = n % 10;
        reversed = reversed * 10 + remainder;
        n /= 10;
    }
    
    printf("The reversed number is %d.", reversed);
    
    return 0;
}

Let’s take a simple example to understand the flow of execution of the above program:

Assume that the number entered by the user is 123. So, n = 123. 

  • In iteration 1
    • As n is non-zero, therefore the while condition is true. 
    • 123 % 10 is 3. This trick is used to get the last digit from a number. This will be the first digit of the reversed number. So, the remainder is 3.
    • Reversed variable is representing the reversed number. Currently, it is zero.
      • Multiplication by 10 gives us the room to add an extra digit at the end of the number. Let’s say the reversed number 32, then multiplication by 10 will give 320. 
      • Currently, reversed is zero, so multiplication has no effect.
      • Simply, the remainder is added to the reversed number which gives 3. This is the first digit of the reversed number. 
    • Value of n is now divided by 10 and the quotient obtained is the new value of n. In our example, the quotient will be 12. In this way, we are truncating the last digit from the original number so that in the next iteration, the second last digit is added at the end of the reversed number. 
  • In iteration 2,
    • n is again non-zero, hence the while condition is true. 
    • 12 % 10 = 2, so remainder = 2. 
    • The old value of reversed is 3.
      • 3 is multiplied by 10 which is 30.
      • And 30 is added with the remainder which gives 32. This is our new reversed number. 
    • n is divided by 10 and the quotient is our new n which is 1. 
  • In iteration 3
    • n is again non-zero, hence the while condition is true. 
    • 1 % 10 = 1, so remainder = 1. 
    • The old value of reversed is 32.
      • 32 is multiplied by 10 which is 320.
      • And 320 is added with the remainder which gives 321. This is our new reversed number. 
    • n is divided by 10 and the quotient is our new n which is 0. 
  • In iteration 4
    • The while condition turns out to be false because the value of n is zero. Hence, the loop terminates. 

The reversed number is 321 and that’s what we want. 



Leave a comment

Leave a comment

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

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.