-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvancedClassificationLoop.c
70 lines (44 loc) · 1.29 KB
/
advancedClassificationLoop.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
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <math.h>
#include "NumClass.h"
/* will return if a number is Armstrong number
An Armstrong number is an n-digit number that is equal to the sum of the nth powers of its digits.
For Example: 407 = 43 + 03 + 73 = 64 + 0 + 343 = 407
*/
int isArmstrong(int num)
{
int originalNum, remainder, n = 0;
float result = 0.0;
originalNum = num;
// store the number of digits of num in n
for (originalNum = num; originalNum != 0; ++n) {
originalNum /= 10;
}
for (originalNum = num; originalNum != 0; originalNum /= 10) {
remainder = originalNum % 10;
// store the sum of the power of individual digits in result
result += pow(remainder, n);
}
// if num is equal to result, the number is an Armstrong number
if ((int)result == num)
return 1;
else
return 0;
}
/* will return if a number is a palindrome */
int isPalindrome(int n)
{
int reversed = 0, remainder, original;
original = n;
// reversed integer is stored in reversed variable
while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
// palindrome if orignal and reversed are equal
if (original == reversed)
return 1;
else
return 0;
}