/*Perfect no. is a +ve integer that is equal to the sum of its +ve divisors
e.g. 28 is divible by 1,2,4,7,14 (excluding number itself)
1+2+4+7+14=28
*/
#include<stdio.h>
#include<conio.h>
void print_perfect(int,int); //function declration
void main()
{
clrscr(); //Clear Screen
print_perfect(1,50); //This Function print perfects number
getch();
}
void print_perfect(int start,int end)
{
int div,sum; //div for divisor, s for summation
int num=start; //num for number under checking
while(start<=end) //loop from 1 to 50
{
div=1;sum=0; //factor starts from 1,summation from zero
while(div<num) //divisor must be less than number itself
{
if(num%div==0) //it checks for divison
{
sum=sum+div; //if it remainder is zero, divisor is added
}
div++; //next divisor
}
if(sum==num) printf("perfect no =%d\n",num); //if summation equal to number itself, it is perfect no. so print it
num++; // next number
}
}//end of function
//End of the program
e.g. 28 is divible by 1,2,4,7,14 (excluding number itself)
1+2+4+7+14=28
*/
#include<stdio.h>
#include<conio.h>
void print_perfect(int,int); //function declration
void main()
{
clrscr(); //Clear Screen
print_perfect(1,50); //This Function print perfects number
getch();
}
void print_perfect(int start,int end)
{
int div,sum; //div for divisor, s for summation
int num=start; //num for number under checking
while(start<=end) //loop from 1 to 50
{
div=1;sum=0; //factor starts from 1,summation from zero
while(div<num) //divisor must be less than number itself
{
if(num%div==0) //it checks for divison
{
sum=sum+div; //if it remainder is zero, divisor is added
}
div++; //next divisor
}
if(sum==num) printf("perfect no =%d\n",num); //if summation equal to number itself, it is perfect no. so print it
num++; // next number
}
}//end of function
//End of the program
No comments:
Post a Comment