-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path32-reverse-integer.cpp
56 lines (42 loc) · 1.06 KB
/
32-reverse-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 <iostream>
using namespace std;
int RevInt(int N) {
// For dealing with -ve integers:
bool isNeg = false;
if (N < 0){
N = -N;
isNeg = true;
}
// Now the actual code:
int multiplyer;
int ans = 0;
// Multipluer will go from max value to 10. eg: 1000 100 10 1
// the follopwing will find its max value
for (int i = 1; ;i *= 10){
if(N / i == 0){
multiplyer = i / 10 ; // suppose N = 9785, then. N/10000 = 0, but for multiplyer, we would take 1000.
break;
}
}
// for making the reversed integer
while (N >0){
int digit = N % 10;
ans += digit * multiplyer;
multiplyer /= 10;
N /= 10;
}
// OUPPUT:
if (isNeg) {
return -ans;
}
else {
return ans;
}
}
int main (){
int num;
cout << "Enter an integer: "; cin >> num;
cout << "The reversed integer is: " << RevInt(num);
return 0;
}
// Mohammad Maasir | 9 sep 2023 | 19:45