-
Notifications
You must be signed in to change notification settings - Fork 3
/
46thProgram_Example_2.cpp
112 lines (101 loc) · 2.68 KB
/
46thProgram_Example_2.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* ********
Armstrong Number :
A number is an Armstrong number if the sum of the power of its digits is equal to the
number itself.
*********/
#include<iostream>
using namespace std;
int calcPower(int,int); //function declaration
int main(){
cout << "Enter a number: ";
int num, m,n, s=0,arm=0, power=0;
cin >> num;
int temp = num;
int temp2 = num;
while(temp>0){
m = temp%10;
temp = temp/10;
s = s+1;
}
for(int i=0;i<s;i++){
n = num%10;
arm = calcPower(n,s) + arm;
num = num/10;
}
if(arm == temp2){
cout << "The number is an armstrong number." << endl;
}
else{
cout << "The number is not an armstrong number." << endl;
}
return 0;
}
//function definition
int calcPower(int base, int exponent){
int result = 1;
for(int i=0;i<exponent;i++){
result = result * base;
}
return result;
}
/****************************************************************
* Digits calculation
----------------------------------------------------------------
Workings:
----------------------------------------------------------------
Suppose num =153
1st process:
m = 153%10 = 3
num = 153/10 = 15
s = 0+1 = 1
----------------------------------------------------------------
2nd process:
----------------------------------------------------------------
m = 15%10 = 5
num = 15/10 = 1
s = 1+1 = 2
----------------------------------------------------------------
3rd process:
----------------------------------------------------------------
m = 1%10 = 1
num = 1/10 = 0
s = 2+1 = 3
----------------------------------------------------------------
Therefore length of digit is : 3
----------------------------------------------------------------
Process 1 :
Enter a number : 153
length of digits : 3
when int i = 0 to s-1 i.e. 0,1,2
i=0
n =153 % 10 = 3
arm = calcPower(3 * 3 ) + 0
= 27
num = num/10 = 153/10 = 15;
---------------------------------------------
Power Calculation
---------------------------------------
int calcPower(base =3,exponent =3){
int result = 1;
for (i=0; i < exponent = 3;i++){
result = 1* base;
i.e.
int i = 0 ; result = 1 * 3 = 3;
int i = 1 ; result = 3 * 3 =9;
int i = 1 ; result = 9* 3 = 27;
}
return result = 27;
}
Similarly,
i = 1
n =15 % 10 = 5
arm = calcPower(5 * 3 ) + 27
= 152
num = 15/10 =1;
i = 2
n =1 % 10 = 1
arm = calcPower(1 * 3 ) + 152
= 1+152 =153
num = 1/10 =0;
Hence arm = temp2 = 153 is an armstrong number
* ***************************************************/