
Reverse a number in C.
Problem Statement: Given a number N reverse the number and print it.
Examples:
Example 1: Input: N = 123 Output: 321 Explanation: The reverse of 123 is 321 Example 2: Input: N = 234 Output: 432 Explanation: The reverse of 234 is 432
Disclaimer: Don’t jump directly to the solution, try it out yourself first.
The idea is to extract digits from the end of the given number and create a new number in reverse order.
How to extract digits from the end of a number?
To extract the last digit, if you divide a number by 10, then the remainder will be the last digit. We can simply use the modulo(%) operator to do this, for example the last digit of 123 will be (123 % 10), which is 3.
To reduce the number by one digit from the end, simply divide the number by 10. example: to reduce 123 to 12, simply do (123/10) which is equal to 12.
To create a number from digits: The idea is to start with 0, and for every digit, multiply the number generated so far by 10, and add the digit to it.
For example, to create a number from digits: [1,2,3]:
Consider the number, num = 0.
Then,
For first digit: 1 num = num*10 + 1 = 0*10 + 1 = 1; For second digit: 2 num = num*10 + 2 = 1*10 + 2 = 12; For third digit: 3 num = num*10 + 2 = 12*10 + 3 = 123;
Approach:
- Run a while loop until the given number N is equal to zero while(N!=0)
- Initialize a variable reverse = 0;
- now in each step take the remainder of the given number N and store it as a variable digit, digit = N % 10
- Also, Divide the number by 10. N= N / 10
- in each step, the variable reverse get updated as reverse = reverse*10+digit.
Code:
C Program
#include<stdio.h>
int main()
{
int N = 123;
int num = N;
int reverse = 0;
while(N!=0)
{
int digit = N%10;
reverse = reverse*10+digit;
N = N/10;
}
printf("The reverse of the %d is %d",num,reverse);
}
Output: The reverse of the 123 is 321
Time Complexity: O(n), where n is the length of the given number
Space Complexity: O(1)
Python Code
if __name__ == '__main__':
N = 123
num = N
reverse = 0
while N != 0:
digit = N % 10
reverse = reverse * 10 + digit
N = N // 10
print("The reverse of the {} is {}".format(num, reverse))
Output: The reverse of the 123 is 321
Time Complexity: O(n), where n is the length of the given number
Space Complexity: O(1)
Special thanks to Subhrajit Das for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article