Write a function “perfect()” that determines if parameter number is a perfect number. Use this function in a program that determines and prints all the perfect numbers between 1 and 1000
#include<stdio.h> #include<math.h> int main() { int a; printf("Enter the number = "); scanf("%d",&a); ///asking limit of the function perfect(a); ///calling the function } perfect(int a) { int i,j,k,s,x; for(x=1;x<=a;x++) ///for numbers from 1 to x { s=0; for(i=1;i<x;i++) ///for each number for factors { for(j=1;j<x;j++) { k=i*j; ///checking whether it is factor or not like 2*3 =6 if(k==x) { s=s+i+j; ///adding all the factors except 1 and itself } } } s=s/2; /// to avoid repetition lke 2*3 and 3*2 s=s+1; /// adding 1 since 1 is also factor if(s==x) ///checking whether it is perfect number or not 6=1+2+3 { printf(" %d ,",x); ///printing each perfect number } } }
Write a function “perfect()” that determines if parameter number is a perfect number. Use this function in a program that determines and prints all the perfect numbers between 1 and 1000
Reviewed by Unknown
on
03:36
Rating:
No comments: