 C Programs - eitworld

# C Program to find sum of two numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int number,perfect=0,d=0,num;
clrscr();
printf("Enter a number:");
scanf("%d",&number);
num=number-1;
while(num>0)
{
if(number%num==0)
{
d=num;
}
perfect=perfect+d;
num--;
}
if(number==perfect)
{
printf("%d is a perfect number",number);
}
else
{
printf("%d is not a perfect number",number);
}
getch();
}

## Explanation:

The above program will print that whether the given number is perfect or not. . The program takes an integer value from user and assigns that value in the variable 'number' and further it assigns a number one-less than the value present in number variable in another variable num. While loop will be executed till the num does not become less than 0.In the loop we will calculate the sum of the positive factors of the given number. If their sum including 1 is equal to number then the number is perfect otherwise not. We can understand this process with the help of given example:

number=6 ,perfect=0,d=0
num=number-1=5
num>0,True
iteration 1:
6%5=0,False
perfect=0+0=0
num=4
num>0,True
iteration 2:
6%4=0,False
perfect=0
num=3
num>0,True
iteration 3:
6%3=0,True
d=num=3
perfect=(perfect+d)=0+3=3
num=2
num>0,True
iteration 4:
6%2=0,True
d=num=2
perfect=3+2=5
num=1
num>0,True
iteration 5:
6%1=0,True
d=num=1
perfect=5+1=6
num=0
num>0,False
loop break
number=perfect=6
so number 6 is a perfect number.
The output of the above program is given below: