Q:

C program to reverse digits of an integer with overflow handled

belongs to collection: C Programming on Numbers

0

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,

Input : num = 12345
Output : 54321
 
Input : num = 1000000045
Output : WARNING OVERFLOWED!!!

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

First, let see a simple C program to reverse the digits of an integer.

#include <stdio.h>
//Recursive function to
//reverse digits of number
int reversDigits(int num)
{
    static int rev_num = 0;
    static int base_pos = 1;
    if(num > 0)
    {
        reversDigits(num/10);
        rev_num += (num%10)*base_pos;
        base_pos *= 10;
    }
    return rev_num;
}
int main()
{
    int number, reversed = 0;
    //Input a number from user
    printf("Enter any number = ");
    scanf("%d", &number);
    reversed = reversDigits(number);
    printf("Reverse of no. is %d", reversed);
    return 0;
}

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.

int reversDigits(int num)
{
    int tmp = 0;
    //If num is negative, then convert it to positive
    tmp = (num < 0)? (-1 * num): num;
    int prev_rev_num = 0, rev_num = 0;
    while (tmp != 0)
    {
        int last_digit = tmp%10;
        prev_rev_num = (rev_num*10) + last_digit;
        // checking if the reverse overflowed or not.
        // The values of (rev_num - last_digit)/10 and
        // prev_rev_num must be same if there was no
        // problem.
        if ((prev_rev_num - last_digit)/10 != rev_num)
        {
            printf("WARNING OVERFLOWED!!!\n");
            return 0;
        }
        else
        {
            rev_num = prev_rev_num;
        }
        tmp = tmp/10;
    }
    return (num < 0)? -rev_num : rev_num;
}
int main()
{
    int number, reversed = 0;
    //Input a number from user
    printf("Enter any number = ");
    scanf("%d", &number);
    //Reverse the number
    reversed = reversDigits(number);
    if(reversed != 0)
    {
        printf("Reverse of no. is %d", reversed);
    }
    return 0;
}

Output:

Enter any number = 12345
Reverse of no. is 54321

Enter any number = 1000000045
WARNING OVERFLOWED!!!

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Programming on Numbers

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C Program to find reverse of a number using a func... >>
<< C program to count number of digits in a number...