-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path031ArmstrongNumberUsingWhile.cpp
52 lines (40 loc) · 1.17 KB
/
031ArmstrongNumberUsingWhile.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
//armstrong number using while loop
// inlcuding required header files
#include <iostream>
using namespace std;
// main function
int main() {
// input a number and initializing variables
int num, tempNum, remainder, digits = 0, result = 0, tempDigits = 0;
cout << "Enter an integer: ";
cin >> num;
tempNum = num;
// couting the digits of a number
while (tempNum != 0) {
tempNum /= 10;
digits++;
}
tempNum = num;
cout << endl << "since, " << endl;
// calculating the result of a number
while (tempNum != 0) {
remainder = tempNum % 10;
if (tempDigits < 2) {
cout << remainder << "^" << digits <<" + ";
} else {
cout << remainder << "^" << digits << " ";
}
result += pow(remainder, digits);
tempNum /= 10;
tempDigits++;
}
// checking weather a given number is Armstrong number or not
if (result == num) {
cout << "= " << num << endl;
cout << num << " is an Armstrong number." << endl;
} else {
cout << "!= " << num << endl;
cout << num << " is not an Armstrong number." << endl;
}
return 0;
}