You are given an integer n. Return the integer formed by placing the digits of n in reverse order.
Input: n = 25
Output: 52
Explanation: Reverse of 25 is 52.
Input: n = 123
Output: 321
Explanation: Reverse of 123 is 321.
Input: n = 54
Given a number, it can be reversed if all the digits are extracted from the end of the original number and pushed at the back of a new reversed number.
revNum = (revNum * 10) + digit
.#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// Function to reverse given number n
int reverseNumber(int n) {
/* After the code, revNum will
contain the reversed number */
int revNum = 0;
/* Keep on iterating while there
are digits left to extract */
while(n > 0) {
int lastDigit = n % 10;
/* Pushing last digit at the
back of reversed number */
revNum = (revNum * 10) + lastDigit;
n = n / 10;
}
return revNum;
}
};
int main()
{
int n = 6678;
/* Creating an instance of
Solution class */
Solution sol;
// Function call to reverse the digits in n
int ans = sol.reverseNumber(n);
cout << "The reverse of given number is: " << ans;
return 0;
}
class Solution {
// Function to reverse given number n
public int reverseNumber(int n) {
/* After the code, revNum will
contain the reversed number */
int revNum = 0;
/* Keep on iterating while there
are digits left to extract */
while (n > 0) {
int lastDigit = n % 10;
/* Pushing last digit at the
back of reversed number */
revNum = (revNum * 10) + lastDigit;
n = n / 10;
}
return revNum;
}
public static void main(String[] args) {
int n = 6678;
/* Creating an instance of
Solution class */
Solution sol = new Solution();
// Function call to reverse the digits in n
int ans = sol.reverseNumber(n);
System.out.println("The reverse of given number is: " + ans);
}
}
class Solution:
# Function to reverse given number n
def reverseNumber(self, n):
""" After the code, revNum will
contain the reversed number """
revNum = 0
""" Keep on iterating while there
are digits left to extract """
while n > 0:
lastDigit = n % 10
""" Pushing last digit at the
back of reversed number """
revNum = (revNum * 10) + lastDigit
n = n // 10
return revNum
if __name__ == "__main__":
n = 6678
""" Creating an instance of
Solution class """
sol = Solution()
# Function call to reverse the digits in n
ans = sol.reverseNumber(n)
print("The reverse of given number is:", ans)
class Solution {
// Function to reverse given number n
reverseNumber(n) {
/* After the code, revNum will
contain the reversed number */
let revNum = 0;
/* Keep on iterating while there
are digits left to extract */
while (n > 0) {
let lastDigit = n % 10;
/* Pushing last digit at the
back of reversed number */
revNum = (revNum * 10) + lastDigit;
n = Math.floor(n / 10);
}
return revNum;
}
}
const n = 6678;
/* Creating an instance of
Solution class */
const sol = new Solution();
// Function call to reverse the digits in n
const ans = sol.reverseNumber(n);
console.log("The reverse of given number is:", ans);
Time Complexity: O(log10(N)) – In every iteration, N is divided by 10 (equivalent to the number of digits in N.)
Space Complexity: O(1) – Using a couple of variables i.e., constant space.