
Here in this program, we will write a code which checks whether the entered number is a Perfect number or not.
What is a Perfect number?
When a positive integer is equal to the sum of its all possible divisors except itself, is known as a perfect number.
For example, 6 is a perfect number because the sum of its all divisors is equal to 6
Divisors of 6 are 1, 2, 3 exclude 6.
Sum of 6 all divisors = 1+2+3 = 6
Statements to use:
- For loop
- If statements
- Else statement
Steps:
- First, we will ask the user to enter a positive integer
- Then we start a loop from 1 to the half of the entered number
- Inside the loop, we will check which number is the divisor of the entered number.
- If the number is a divisor of the entered number, we will sum it.
- At last, we will check whether the summing of all the divisors is equal to the entered number or not
- If it is equal to the entered number, we will print It is a perfect number, is not we will print it is not a perfect number.
Perfect Number in C
#include<stdio.h> #include<conio.h> void main() { int num, sum=0,i; clrscr(); printf("Enter a Positive Integer: "); scanf("%d",&num); if (num==1) { printf("It is not a Perfect number"); } else { for(i=1;i<=num/2;i++) { if(num%i==0) sum+=i; } if(sum==num) printf("It is a perfect number"); else printf("It is not a perfect number"); } getch(); }
Output:
Enter a Positive Integer: 6
It is a perfect number
People Aare also reading:
- WAP in C++ & Python to reverse a number
- Python Program to Print Hello world!
- WAP in C++ & Python for Armstrong Number
- Write a Program in C to Check Whether the Number is a Prime or not
- How to Find Square Root in Python?
- WAP in C++ & Python to calculate the size of each data types
- Python Program to Convert Celsius To Fahrenheit
- WAP in C++ & Python to Calculate Compound Interest
- Python Program to Check Whether a String is Palindrome or Not
- WAP in C++ & Python to Insert an Element in an Array