Home » C++ Tutorial » Reverse Integer

Reverse Integer


Reading Time - 4 mins


Difficulty Level Easy

Problem Statement

“Reverse Integer” problem states that you are given an integer variable n containing the number to be reversed. Write a program to reverse its digits.

Reversing an integer is nothing different from reversing a string. We can easily convert an integer into a string. And then use the various methods to reverse a string. But instead of doing that there is another method to complete our task. Because the first conversion of an integer to string is an overhead. So, instead of taking look at those solutions. We will be looking at the new method.

Example

Reverse Integer

13567
76531

Explanation: On reversing each of the digits of the given number. That is replacing the last digit with the first digit of the number and replacing the first digit with the last digit. We process each digit of the number in a similar fashion.  We get 76531 as result.

578
875

Explanation: More formally what we do is if we give an index number to each digit. We have considered 0 based indexing and we are calling the number as n. Then we swap place the n[0] at n[2] and n[2] at n[0]. Then the process repeats until the middle. This is just one of the ways to reverse the input.

READ  C++ Variable Types

Iterative Method

Algorithm to reverse integer iteratively

1. Initialize an integer n containing the number to be reversed.
2. Create a function to reverse a number which accepts an integer variable as it's a parameter.
3. Initialize an integer variable rev as 0 to store the reverse of the given number.
4. After that, traverse through the given number. While the given number is greater than 0,  multiply the integer rev with 10 and add it to the last digit of the given number and store it in the variable rev itself. Update the given number as the number itself divide by 10 to discard the last digit.
5. Finally, return the variable containing the reversed number and print it.

Code

Iterative C++ Program to reverse integer

#include <bits/stdc++.h> 
using namespace std; 

int revDigits(int n){ 
    int rev = 0; 
    while(n > 0){ 
        rev = rev*10 + n%10; 
        n = n/10; 
    }
    return rev; 
} 
int main(){ 
    int n = 4562; 
    cout<<revDigits(n);
    return 0; 
} 
2654

Iterative Java Program to reverse integer

class reverse{ 
    static int revDigits(int n){ 
        int rev = 0; 
        while(n > 0){ 
            rev = rev*10 + n%10; 
            n = n/10; 
        } 
        return rev; 
    } 
    public static void main (String[] args){ 
        int n = 4562; 
        System.out.println(revDigits(n)); 
    } 
}
2654

Complexity Analysis

Time Complexity

O(log(n)) where n is the number of digits in the given integer. Because there are only log(n) digits in a number n.

Space Complexity

O(1) because we used constant space.

Recursive Method

Algorithm to reverse integer recursively

1. Initialize an integer n containing the number to be reversed.
2. Create a function to reverse a number which accepts an integer variable as it's a parameter.
3. Initialize an integer variable rev as 0 to store the reverse of the given number.
4. Similarly, initialize an integer variable pos as 1 to store the base position of the digit.
5. After that, check if the given number is greater than 0, call the function itself with a given number divided by 10 as it's a parameter. Add the last digit of the given number multiplied by the variable pos in variable rev. Update the variable pos as the multiplication of 10 with variable pos itself.
6. Finally, return the variable containing the reversed number and print it.

Code

Recursive C++ Program to reverse integer

#include <bits/stdc++.h> 
using namespace std; 

int revDigits(int n){ 
    static int rev = 0, pos = 1; 
    if(n > 0){ 
        revDigits(n/10); 
        rev += (n%10)*pos; 
        pos *= 10; 
    } 
    return rev; 
}  
int main(){ 
    int n = 4562; 
    cout<<revDigits(n); 
    return 0; 
} 
2654

Recursive Java Program to reverse integer

class reverse{ 
    static int rev = 0, pos = 1;
    static int revDigits(int n){ 
        if(n > 0){ 
            revDigits(n / 10); 
            rev += (n % 10) * pos; 
            pos *= 10; 
        } 
        return rev; 
    } 
    public static void main (String[] args){ 
        int n = 4562; 
        System.out.println(revDigits(n)); 
    } 
}
2654

Complexity Analysis

Time Complexity

O(log(n)) where n is the number of digits in the given integer. Because there are only log(n) digits in a number n.

Space Complexity

O(1) because we used constant space.

Array Interview Questions
Graph Interview Questions
LinkedList Interview Questions
String Interview Questions
Tree Interview Questions