First of all we all should know what is perfect number?
In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself.
Example: 6, divisor of 6 are 1,2,3. Sum of divisors is 1+2+3=6.
PROGRAM
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>
int main(){
int n,i,sum;
printf("Perfect numbers are: ");
for(n=1;n<=100;n++){
i=1;
sum = 0;
while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d ",n);
}
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include<stdio.h> int main(){ int n,i,sum; printf("Perfect numbers are: "); for(n=1;n<=100;n++){ i=1; sum = 0; while(i<n){ if(n%i==0) sum=sum+i; i++; } if(sum==n) printf("%d ",n); } return 0; } |
0 comments:
Post a Comment