-
Notifications
You must be signed in to change notification settings - Fork 2
/
Check_Armstrong_Number.c
63 lines (50 loc) · 1.19 KB
/
Check_Armstrong_Number.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
54
55
56
57
58
59
60
61
62
//
// main.c
// Check Armstrong Number
//
// Created by dabbaghıe on 28.11.2019.
// Copyright © 2019 dabbaghıe. All rights reserved.
//
#include <stdio.h>
#include <stdbool.h>
int power(int x, unsigned int y)
{
if (y == 0)
return 1;
else if (y%2 == 0)
return power(x, y/2)*power(x, y/2);
else
return x*power(x, y/2)*power(x, y/2);
}
bool IsArmstrong(int number )
{
int originalNumber, remainder,n=0, result = 0;
originalNumber = number;
while (originalNumber != 0)
{
originalNumber /= 10;
++n;
}
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber%10;
result += power(remainder, n);
originalNumber /= 10;
}
if(result == number)
return true;
return false;
}
int main(int argc, const char * argv[]) {
// insert code here...
int number;
printf("Enter a three digit integer: ");
scanf("%d", &number);
bool t=IsArmstrong(number);
if(t == true)
printf("%d is an Armstrong number.\n",number);
else
printf("%d is not an Armstrong number.\n",number);
return 0;
}