forked from iamAnki/CPP-Programs-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReversing_Digits_of_an_integer.cpp
56 lines (46 loc) · 1.15 KB
/
Reversing_Digits_of_an_integer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <bits/stdc++.h>
using namespace std;
/* Iterative function to reverse
digits of num*/
int reversDigits(int num)
{
// Handling negative numbers
bool negativeFlag = false;
if (num < 0)
{
negativeFlag = true;
num = -num ;
}
int prev_rev_num = 0, rev_num = 0;
while (num != 0)
{
int curr_digit = num % 10;
rev_num = (rev_num * 10) + curr_digit;
// checking if the reverse overflowed or not.
// The values of (rev_num - curr_digit)/10 and
// prev_rev_num must be same if there was no
// problem.
if ((rev_num - curr_digit) /
10 != prev_rev_num)
{
cout << "WARNING OVERFLOWED!!!"
<< endl;
return 0;
}
prev_rev_num = rev_num;
num = num / 10;
}
return (negativeFlag == true) ?
-rev_num : rev_num;
}
// Driver Code
int main()
{
int num = 12345;
cout << "Reverse of no. is "
<< reversDigits(num) << endl;
num = 1000000045;
cout << "Reverse of no. is "
<< reversDigits(num) << endl;
return 0;
}