Photo by on 愚木混株 cdd20 Unsplash Abstract Reversing a number in the C/C++ program means interchanging the digits i.e. to bring the last digit of the given number to the first position or vice versa. Scope of the Article This article provides a basic understanding regarding reversing a number and the algorithm for the same. Different methods to implement the same in C. Prerequisites One should have a basic understanding of C/C++ Understanding regarding how loop and recursion work. Introduction As we know that reversing a number means interchanging the digits so that the last digit of the number comes first or vice versa. Let us understand the same with the help of an example:- Given input is 56897. Then reverse of the given number would be 79865. As we have understood what is meant by reversing a number, let us go further into understanding its algorithm. Algorithm to Reverse a Number in C Let’s consider the given number to be num = . Consider reverse number be Step 1. 5689 rev_num =0. . Now we will use the given formula, ie. Step 2 = + ; 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 r by multiplying the same with . ev_num 10 To avoid extracting the same last digit from num, we will update num by dividing it by and storing that value in . 10 num . We will continue the procedure until the given number becomes less than or equal to . Step 3 0 Once we reached that stage then we have obtained our reverse number. 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 Different Methods to Implement the Same in C Here we use 2 arithmetic operators namely modulo and division operator. An iterative method of reversing a number # 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 Recursive Implementation of Reversing a Number # 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; } Output enter the number you wish to reverse: 54689 reverse of the given number 54689 is : 98645 Explanation In recursive function, represents the reverse of the given number. revnum We have put a condition that if the given number represented by ‘n’ becomes zero, then we will return our reverse number. In all other cases, we will call (n/10, n % 10 + k*10) with updated values of n and respectively reversenumber revnum 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 Using Function # 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 What if a negative number is given as input? As we know that computer handles only binary numbers, unlike humans which can able to interpret multiple languages. The binary number representation of negative numbers is different from positive numbers. Thus if we directly apply the above algorithm to a negative number then it will produce a wrong answer. To avoid such a case, we will multiply the said negative number with -1 to make it a positive number and then apply the algorithm to make it reverse, and after that, we will again multiply -1 to the reverse number. 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; } Handle Overflow Cases While Reversing a Number As we know integer can handle up to 32 bits. It may so happen that the reverse of the number may exceed or overflow 32 bit. In such a case check every time whether the reverse number exceeds the 32 bit. 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, would contain garbage value, which is not going to be equal to ( - )/ . rev_num prev_num rem 10 Analysis of the Algorithm Time Complexity While loop or recursive function executes the program for the number of digits present in number. A number of digits in number num have the computing of log (num) to base 10. So time complexity is O(log10(num)). Space Complexity We have not used any extra space. Thus space complexity is O(1). Conclusion In this article, we have seen the algorithm of reversing a number and different methods through which it can be implemented in a C-like loop, recursive. Along with it can we have seen some corner cases like if given input is negative or reverse of a number exceeds 32 bit. To further enhance your knowledge on reversing a number and its usage with beautiful schematics, one may want to have a look at this curated article published on the on Scaler topics. C Program to Reverse a Number