forked from abhijithshaji17/cpp-mastery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9.cpp
More file actions
31 lines (28 loc) · 660 Bytes
/
Copy path9.cpp
File metadata and controls
31 lines (28 loc) · 660 Bytes
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
// Reverse digits of a number
#include<iostream>
using namespace std;
int main(){
int n,i,rev=0;
cout<<"Enter a no.: ";
cin>>n;
for(i=0;n!=0;i++){
rev = n%10 + rev*10;
n/=10;
}
cout<<"Reversed number = "<<rev<<endl;
return 0;
}
/*
Loop Trace(n=67):
Initial Setup: n = 67, rev = 0
Iteration 1 ->
Find the last digit: n % 10 => 67 % 10 = 7
Update rev: rev = (0 * 10) + 7 = 7
Drop the last digit: n = 67 / 10 = 6
Iteration 2 ->
Find the last digit: n % 10 => 6 % 10 = 6
Update rev: rev = (7 * 10) + 6 = 76
Drop the last digit: n = 6 / 10 = 0
Reversed no. = 76
*/
// !! Careless misunderstanding: 6/10 10/6 6%10 10%6