-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvancedClassificationLoop.c
More file actions
69 lines (62 loc) · 1.04 KB
/
advancedClassificationLoop.c
File metadata and controls
69 lines (62 loc) · 1.04 KB
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
#include <stdio.h>
#include "NumClass.h"
//calc how many digits there are in x
int digAmount(int x){
if(x==0){
return 1;
}
int sum = 0;
while(x>0){
x = x/10;
sum +=1;
}
return sum;
}
//calc x^pow
int Pow(int x, int pow){
if(pow == 0){
return 1;
}
return x*Pow(x,pow-1);
}
//check if x is armstrong number
int isArmstrong(int x){
int digits = digAmount(x);
int sum = 0;
int num = x;
for(int i=1; i<=digits; i+=1){
sum += Pow(x%10,digits);
x = x/10;
}
if(sum==num){
return true;
}
return false;
}
//gets the i digit of x
int getDig(int x, int i){
int d = x/Pow(10,i);
return d%10;
}
int isPalindrome(int x){
int digits = digAmount(x);
if(digits == 1){
return true;
}
for(int i=0; i<digits/2; i+=1){
if(getDig(x,i) != getDig(x,digits-1-i)){
return false;
}
}
return true;
}
/*
int main()
{
for(int i = 0; i < 100; i+=1){
printf("%d: is palindrome? %d \n" ,i,isPalindrome(i));
printf("%d: is armstrong? %d \n" ,i,isArmstrong(i));
printf("------------------------\n");
}
return 0;
}*/