-
Notifications
You must be signed in to change notification settings - Fork 253
/
Interchange_First_and_Last_Digit_of_a_Number.cpp
71 lines (71 loc) · 1.6 KB
/
Interchange_First_and_Last_Digit_of_a_Number.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
#include<iostream>
using namespace std;
int main()
{
int num, rem, temp, rev=0, noOfDigit=0;
int noOfDigitTemp, revNum, remTemp;
cout<<"Enter the Number: ";
cin>>num;
temp = num;
while(temp>0)
{
temp = temp/10;
noOfDigit++;
}
if(noOfDigit<2)
{
cout<<"\nIt is a Single-digit Number!";
cout<<"\nTry again with a Number with Two or More Digits!";
}
else if(noOfDigit==2)
{
temp = num;
while(temp>0)
{
rem = temp%10;
rev = (rev*10)+rem;
temp = temp/10;
}
cout<<"\nFirst and Last Digit Interchanged Successfully!";
cout<<"\n\nNew Number = "<<rev;
}
else
{
temp = num;
while(temp>0)
{
rem = temp%10;
rev = (rev*10)+rem;
temp = temp/10;
}
revNum = rev;
rev = 0;
temp = num;
noOfDigitTemp = noOfDigit;
while(temp>0)
{
remTemp = revNum%10;
if(noOfDigitTemp==noOfDigit)
{
rem = temp%10;
rev = (rev*10)+rem;
}
else if(noOfDigitTemp==1)
{
rem = temp%10;
rev = (rev*10)+rem;
}
else
{
rev = (rev*10)+remTemp;
}
temp = temp/10;
revNum = revNum/10;
noOfDigitTemp--;
}
cout<<"\nFirst and Last Digit Interchanged Successfully!";
cout<<"\n\nNew Number = "<<rev;
}
cout<<endl;
return 0;
}