-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy path5_CheckArmstrong.c
74 lines (37 loc) · 1.05 KB
/
5_CheckArmstrong.c
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
/*
Check if the Number is Armstrong
Author : Krishna Teja G S
Repository : github.com/packetprep/coding-questions
Website : packetprep.com
*/
#include<stdio.h>
#include<math.h>
int main(){
int number,lastDigit,originalNumber,sum=0,power=0;
printf("Enter a positive integer: ");
scanf("%d",&number);
// An Armstrong number of three digits is an integer such that the
//sum of the cubes of its digits is equal to the number itself.
//For example, 371 is an Armstrong number since 3^3 + 7^3 + 1^3 = 371.
//save a copy of given number
originalNumber = number;
//count the digits
while(number!=0){
number = number/10;
power++;
}
// reassign number as number changed to 0
number = originalNumber;
//reverse the number
while(number!=0){
//get the last digit
lastDigit = number % 10;
sum = sum + pow(lastDigit,power);
//remove the last digit
number = number/10;
}
if(originalNumber == sum)
printf("The given number is a Armstrong\n");
else
printf("The given number is not a Armstrong \n");
}