mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Merge branch 'master' into scheduling
This commit is contained in:
commit
243dcc156d
@ -1,21 +1,41 @@
|
|||||||
// Program to check whether a number is an armstrong number or not
|
// Program to check whether a number is an armstrong number or not
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <cmath>
|
||||||
using std::cin;
|
using std::cin;
|
||||||
using std::cout;
|
using std::cout;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int n, k, d, s = 0;
|
int n = 0, temp = 0, rem = 0, count = 0, sum = 0;
|
||||||
cout << "Enter a number:";
|
cout << "Enter a number: ";
|
||||||
cin >> n;
|
cin >> n;
|
||||||
k = n;
|
|
||||||
while (k != 0) {
|
temp = n;
|
||||||
d = k % 10;
|
|
||||||
s += d * d * d;
|
/* First Count the number of digits
|
||||||
k /= 10;
|
in the given number */
|
||||||
|
while(temp != 0) {
|
||||||
|
temp /= 10;
|
||||||
|
count++;
|
||||||
}
|
}
|
||||||
if (s == n)
|
|
||||||
cout << n << "is an armstrong number";
|
/* Calaculation for checking of armstrongs number i.e.
|
||||||
else
|
in a n digit number sum of the digits raised to a power of n
|
||||||
cout << n << "is not an armstrong number";
|
is equal to the original number */
|
||||||
|
|
||||||
|
temp = n;
|
||||||
|
while(temp!=0) {
|
||||||
|
rem = temp%10;
|
||||||
|
sum += (int) pow(rem,count);
|
||||||
|
temp/=10;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (sum == n) {
|
||||||
|
cout << n << " is an armstrong number";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
cout << n << " is not an armstrong number";
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user