C - Perfect Number or not
Problem Statement
Brief Description
According to the problem statement we have to write a program in C that will take an integer value as input from the user through the keyboard and will check whether it is a perfect number or not.
A positive number is perfect if it is equal to the number obtained by adding its proper positive divisors excluding the number itself.
For example, if the user enters 6 as input, the program will generate an output indicating that 6 is a perfect number since the number 6 is divisible by 1, 2 and 3 (excluding 6) and 1+2+3=6. But if the user enters 8 as input, the program will generate an output indicating that 8 is not a perfect number since the number 8 is divisible by 1, 2 and 4 (excluding 8) and 1+2+4=7.
Program
#include
void main()
{
int num,i,sum=0;
printf("Enter a number: ");
scanf("%d",&num);
for(i=1;i<=num/2;i++)
{
if(num%i==0)
sum=sum+i; //OR sum+=i;
}
if(num==sum)
printf("%d is a perfect number",num);
else
printf("%d is not a perfect number",num);
}
Output
Output 1
Output 2
Explanation
- 3 integer variable namely num, i and sum are declared.
- Initially 0 is stored in sum.
- The integer value entered by the user is stored in num.
num | i<=num/2 | num%i==0 | sum=sum+i; | sum | i++ OR i=i+1 | i | Comment |
---|---|---|---|---|---|---|---|
6 | ----- | ----- | ----- | 0 | ----- | 1 | Initial Values |
6 | 1<=6/2 ↓ 1<=3 ↓ 1(True) | 6%1==0 ↓ 0==0 ↓ 1(True) | sum=0+1; | 1 | i=1+1 | 2 | Iteration 1 |
6 | 2<=6/2 ↓ 2<=3 ↓ 1(True) | 6%2==0 ↓ 0==0 ↓ 1(True) | sum=1+2; | 3 | i=2+1 | 3 | Iteration 2 |
6 | 3<=6/2 ↓ 3<=3 ↓ 1(True) | 6%3==0 ↓ 0==0 ↓ 1(True) | sum=3+3; | 6 | i=3+1 | 4 | Iteration 3 |
6 | 4<=6/2 ↓ 4<=3 ↓ 0(False) | ----- | ----- | ----- | ----- | ----- | For loop stops working as condition is evaluated to 0(False) |
- At first 1 is stored in i.
- The condition i<=num/2 is evaluated.
- The condition i<=num/2 will produce either 1(True) or 0(False) after evaluation.
- If it is 1(True), the statement written within for loop is executed and the loop continues to iterate till the condition is 1(True).
- If it is 0(False) then the loop stops working and the control goes to the statement written immediately after the loop.
- At the end of each iteration the value of i is incremented by 1 by executing the statement i++.
- Within for loop it is checked to see whether the value stored in i is a proper positive divisor of the value stored in num or not.
- If it is then the value stored in i is added with the value stored in sum and the result is stored in the same variable i.e. sum.
- If it is not then the statement written within if statement is not executed.
Thus the above process is used to add the proper positive divisors of the value stored in num excluding the number itself.
The final result is stored in sum.
Finally it is checked to see whether the value stored in num is equal to the value stored in sum. If it is then we can conclude that the value stored in num is a perfect number otherwise not and an appropriate message is displayed on the output screen.