What is meant by Reversing the number?
If we took a number 4567 then the reverse of the number should be 7654.
What is use of Reversing of the number?
It's very important and is use to recognize the pattern. We use this concept to check or create palindromes.
What should be the approach for this program ?
In this program we will be using the concept of ones tens hundreds and thousand places place value system. Like if 1234 a number is given
then we can find the number by taking the remainder and multiplying the number by 10 to shift its place by one unit like
remainder is 1 then 10+2 , 120+3 , 1230+4 we get that number. So for finding the reverse of the number
we will be using the same concept.
Flowchart for Reversing the Number
Algorithm for Reversing the Number
Declare a variable n, reverse and remainder as integer;
Read the number n;
while n not equal 0
{
remainder=n%10;
reverse=reverse * 10 + remainder;
n=n/10;
}
Print reverse
Here in this algorithm we declare 3 variables n to store the integer , remainder to find the last digit and reverse for storing
the result. So in this algorithm read the number let's say 4536 then we enter the while loop as n is not equal 0.Then we use
modulo to find the last digit and in reverse we multiply the reverse * 10 and add it.
- In the first iteration remainder=6 and reverse = 0 * 10 + 6 and n becomes 453
- In the second iteration remainder=3 and reverse = 6 * 10 + 3 and n becomes 45
- In the second iteration remainder=5 and reverse = 63 * 10 + 5 and n becomes 4
- In the second iteration remainder=4 and reverse = 635 * 10 + 4 and n becomes 0
Now the n will not enter the loop and we print the value of reverse ie. 6354. We can see that by multiplying the number by 10
we are shifting the place by one unit. You can find it tricky but on doing and solving same type of problem it will be easy for you.
Implementation of the Program in C:
#include
int main() {
int n, reverse, remainder;
printf("Enter the number to find its reverse:");
scanf("%d", & n);
while (n != 0) {
remainder = n % 10;
reverse = reverse * 10 + remainder;
n = n / 10;
}
printf("Reverse of the is %d: ", reverse);
}
Output of the program