-
Notifications
You must be signed in to change notification settings - Fork 3
/
48thProgram_Example_4.cpp
71 lines (58 loc) · 1.42 KB
/
48thProgram_Example_4.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//Reverse of a number
#include<iostream>
using namespace std;
int main(){
cout << "Enter a number: ";
int num;
int m;
int digit;
int s=0;
int rev = 0;
//Input
cin >> num;
int temp = num;
int temp1 = num;
int temp2 = num;
while(temp>0){
m = temp%10;
temp = temp/10;
s = s+1;
}
for(int i = 1 ; i<=s; i++){
digit = num % 10;
rev = rev * 10 + digit;
num = num / 10;
}
cout << " The reverse of number:"<<temp2 <<"is:" << rev;
return 0;
}
/* Workings ************************************************
number = 123
calculation of length:
Process 1
----------------------------------------------------------------
m = 123%10 = 3 (remainder)
temp = 123/10 = 12(quotient)
s = 0+1 = 1
Process 2
----------------------------------------------------------------
m = 12%10 = 2 (remainder)
temp = 12/10 = 1(quotient)
s = 1+1 = 2
Process 3
----------------------------------------------------------------
m = 1%10 = 1 (remainder)
temp = 1/10 = 0(quotient)
s = 2+1 = 3
reverse of a number Workings
range : 1 to 3
digit = 123 %10 = 3
rev = 0 * 10 +3 = 3
num = 123/10 = 12
digit = 12 % 10 = 2
rev = 3 * 10 + 2 = 30 +2 = 32
num = 12 / 10 = 1
digit = 1 % 10 = 1
rev = 32 * 10 + 1 = 320+ 1 = 321
num = 1/10 = 0
********************************************/