Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c1e5f07

Browse files
authored
Create Armstrong & Perfect Number Function Variants.c
1 parent f5e9902 commit c1e5f07

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <stdio.h>
2+
3+
int checkArmstrong(int n1);
4+
int checkPerfect(int n1);
5+
6+
int main()
7+
{
8+
int n1;
9+
printf("\n\n Function : check Armstrong and perfect numbers :\n");
10+
printf("-----------------------------------------------------\n");
11+
12+
printf(" Input any number: ");
13+
scanf("%d", &n1);
14+
15+
16+
//Calls the isArmstrong() function
17+
if(checkArmstrong(n1))
18+
{
19+
printf(" The %d is an Armstrong number.\n", n1);
20+
}
21+
else
22+
{
23+
printf(" The %d is not an Armstrong number.\n", n1);
24+
}
25+
26+
//Calls the checkPerfect() function
27+
if(checkPerfect(n1))
28+
{
29+
printf(" The %d is a Perfect number.\n\n", n1);
30+
}
31+
else
32+
{
33+
printf(" The %d is not a Perfect number.\n\n", n1);
34+
}
35+
return 0;
36+
}
37+
38+
// Checks whether a three digits number is Armstrong number or not.
39+
//An Armstrong number is an n-digit number that is equal
40+
//to the sum of the n-th powers of its digits.
41+
int checkArmstrong(int n1)
42+
{
43+
int ld, sum, num;
44+
sum = 0;
45+
num = n1;
46+
while(num!=0)
47+
{
48+
ld = num % 10; // find the last digit of the number
49+
sum += ld * ld * ld; //calculate the cube of the last digit and adds to sum
50+
num = num/10;
51+
}
52+
return (n1 == sum);
53+
}
54+
// Checks whether the number is perfect number or not.
55+
//a perfect number is a positive integer that is equal to
56+
//the sum of its positive divisors excluding the number itself
57+
int checkPerfect(int n1)
58+
{
59+
int i, sum, num;
60+
sum = 0;
61+
num = n1;
62+
for(i=1; i<num; i++)
63+
{
64+
/* If i is a divisor of n1 */
65+
if(num%i == 0)
66+
{
67+
sum += i;
68+
}
69+
}
70+
return (n1 == sum);
71+
}

0 commit comments

Comments
 (0)