-
Notifications
You must be signed in to change notification settings - Fork 0
/
pat1.c
79 lines (58 loc) · 1.31 KB
/
pat1.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
71
72
73
74
75
76
77
78
79
// Write a C program to check whether a number is prime, armstrong, perfect number or not using functions.
// Input:
// 11
// Output:
// 11 is prime number
// 11 is not a armstrong number
// 11 is not a perfect number
#include <stdio.h>
#include <stdbool.h>
bool isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i < num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
bool isArmstrong(int num) {
int sum = 0, r, temp = num;
while (temp > 0) {
r = temp % 10;
sum += (r * r * r);
temp /= 10;
}
return (num == sum);
}
bool isPerfect(int num) {
int sum = 0;
for (int i = 1; i < num; i++) {
if (num % i == 0) {
sum += i;
}
}
return (num == sum);
}
int main() {
int num;
scanf("%d", &num);
if (isPrime(num)) {
printf("%d is prime number\n", num);
} else {
printf("%d is not prime number\n", num);
}
if (isArmstrong(num)) {
printf("%d is a armstrong number\n", num);
} else {
printf("%d is not a armstrong number\n", num);
}
if (isPerfect(num)) {
printf("%d is a perfect number\n", num);
} else {
printf("%d is not a perfect number\n", num);
}
return 0;
}