Skip to content

Commit c5e8c7c

Browse files
committed
add c program to check for perfect numbers
1 parent 80dd25b commit c5e8c7c

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

programs/C/perfe.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include<stdio.h>
2+
3+
int main()
4+
{
5+
int sum = 0, n;
6+
//prompt user for number to check whether it is perfect or not
7+
printf("please enter number to check whether it is perfect or not:");
8+
scanf("%d", &n);
9+
for( int i = 1; i <= n/2; i++){
10+
if( n%i == 0) //find sum of divisors of entered number
11+
sum += i;
12+
}
13+
//check if sum of divisors is equal to entered number or not and then print required output
14+
if( sum == n)
15+
printf("hurray!,entered number is perfect\n");
16+
else
17+
printf("number is not perfect, try another one\n");
18+
}

0 commit comments

Comments
 (0)