visit
Photo by on
Let us understand the same with the help of an example:-
Given input is 56897.
Then reverse of the given number would be 79865.
Step 1. Let’s consider the given number to be num = 5689. Consider reverse number be rev_num =0.
Step 2. Now we will use the given formula, ie.
rev_num = rev_num*10 + num%10;
num = num / 10;
As we can see each time we are extracting the last digit of the given number and accommodate that last digit in rev_num by multiplying the same with 10.
To avoid extracting the same last digit from num, we will update num by dividing it by 10 and storing that value in num.
Step 3 . We will continue the procedure until the given number becomes less than or equal to 0.
In the above example after the 5th iteration, we will obtain our reverse number ie 79865.
Digit extracted from num | Multiplication with 10 i.e. rev_num |
---|---|
7 | 0*10+ 7 =7 |
9 | 7*10 +9 = 79 |
8 | 79*10+8 = 798 |
6 | 798*10+6=7986 |
5 | 7986*10+5=79865 |
# include <stdio.h>
int main(){
int num, rev_num = 0 , rem ;
prinf("enter the number you wish to reverse:");
scanf ("%d",&num);
// using while loop and decreasing num till num >= 0
while (num != 0){
rem = num %10 ;
rev_num = (rev_num *10) + rem;
num = num/ 10 ; //updating the the number by dividing it by 10
}
printf ("reverse of the given number %d is : %d", num , rev_num);
return 0;
}
Output
enter the number you wish to reverse: 54689
reverse of the given number 54689 is : 98645
# include <stdio.h>
int reversenumber(int n, int revnum){
if (n==0) return revnum;
return reversenumber (n/10 , n % 10 + k*10);
}
int main(){
int num, rev_num ;
prinf("enter the number you wish to reverse");
scanf ("%d",&num);
//calling recursive function reversenumber
rev_num = reversenumber(num, 0);
printf ("reverse of the given number %d is : %d", num , rev_num);
return 0;
}
enter the number you wish to reverse: 54689
reverse of the given number 54689 is : 98645
revnum
represents the reverse of the given number.reversenumber
(n/10, n % 10 + k*10) with updated values of n and revnum
respectively
iteration | Value of n | Value of revnum | Return Value |
---|---|---|---|
0 | 54689 | 0 | return(54689,0) |
1 | 5468 | 9 | return(5468,9) |
2 | 546 | 98 | return(546,98) |
3 | 54 | 986 | return (54,986) |
4 | 5 | 9864 | return(5,9864) |
5 | 0 | 98654 | as n==0, then we will simply return value of revnum = 98654 |
# include <stdio.h>
int revnumber( int n){
int rem , rev_num =0;
for (; n> 0; n= n/10){
rem = n%10 ;
rev_num = rev_num*10 + rem;
}
return rev_num;
}
int main(){
int num, rev_num = 0 ;
prinf("enter the number you wish to reverse:");
scanf ("%d",&num);
rev_num = revnumber (num);
printf ("reverse of the given number %d is : %d", num , rev_num);
return 0;
}
Output
enter the number you wish to reverse: 56897
reverse of the given number 54689 is : 79865
Given code will help to better understand this.
# include <stdio.h>
# include <stdbool.h>
int main(){
int num, rev_num = 0 , rem ;
bool neg = false;
prinf("enter the number you wish to reverse:");
scanf ("%d",&num);
if (num < 0){
num = -num ;
neg = true;
}
// using while loop and decreasing num till num >= 0
while (num != 0){
rem = num %10 ;
rev_num = (rev_num *10) + rem;
num = num/ 10 ; //updating the the number by dividing it by 10
}
if (neg){
num = - num;
}
printf ("reverse of the given number %d is : %d", num , rev_num);
return 0;
}
The Below code provides a better understanding.
# include <stdio.h>
int revnumber( int n){
int rem , rev_num =0, prev_num = 0;
while (n ! =0 ){
rem = n%10 ;
prev_num = (rev_num) *10 + rem;
if ((prev_num - rem )/10 != rev){
printf("WARNING REVERSE NUMBER IS OVERFLOWED!!!");
return 0;
}
else rev_num = prev_num;
n= n/10;
}
return rev_num;
}
int main(){
int num, rev_num = 0 ;
prinf("enter the number you wish to reverse:");
scanf ("%d",&num);
rev_num = revnumber (num);
if (rev_num != 0){
printf ("reverse of the given number %d is : %d", num , rev_num);
}
return 0;
}
In this code, we have checked if the reverse number is overflowed. In case of overflowing, rev_num would contain garbage value, which is not going to be equal to (prev_num - rem)/10.