In this exercise, we learn how to write a C program to reverse digits of an integer with overflow handled? Write C program to reverse digits of an integer with overflow handled. How to find reverse of a number with overflow handled in C programming. Write a program to reverse an integer assuming that the input is a 32-bit integer. Let’s see an example,
First, let see a simple C program to reverse the digits of an integer.
Output:
Enter any number = 12345
Reverse of no. is 54321
Enter any number = 1000000045
Reverse of no. is 1105032705
However, if the number is large such that the reverse overflows, the output is some garbage value. If we run the above code with input as any large number say 1000000045, then the output is some garbage value like 1105032705 or any other garbage value.
How to handle overflow?
The idea is to store the previous value of the sum that can be stored in a variable that can be checked every time to see if the reverse overflowed or not.
Note: Assuming input is a 32-bit integer.
Output:
Enter any number = 12345
Reverse of no. is 54321
Enter any number = 1000000045
need an explanation for this answer? contact us directly to get an explanation for this answerWARNING OVERFLOWED!!!