In this tutorial, you will learn how to write a program to check a given number is perfect or not using While loop conditional statement.
What is Perfect Number?
A perfect number is a positive integer that is equal to the sum of its proper divisors (excluding itself). In other words, a perfect number is the sum of its divisors, excluding the number itself.
For Example
The number 28 is a perfect number.
Divisors of 28 are: 1, 2, 4, 7, 14
Sum of divisors: 1 + 2 + 4 + 7 + 14 = 28
Problem Statement
Write a C program to check whether a given number is perfect or not. Program should take a input integer and after computation it should print the output as given input number is perfect or not.
Example
Input: Enter a number: 28
Output: 28 is a perfect number
Algorithm For Checking Perfect Number
- Take input for the number to be checked from the user.
- Initialize a variable
sum
to 0, which will store the sum of the proper divisors of the number. - Initialize a variable divisor to 1, which will be used to check for divisors.
- Enter a while loop with the condition divisor < number to iterate through possible divisors.
- Inside the while loop, check if number is divisible by divisor.
- If number is divisible, add the value of divisor to sum.
- Increment the value of divisor by 1 to move on to the next possible divisor.
- The while loop continues until divisor is less than the given number.
- After the while loop, check if sum is equal to the given number.
- If sum is equal to number, print that the given number is a perfect number.
- Otherwise, print that the given number is not a perfect number.
Program to check a given number is Perfect or not
#include <stdio.h>
int main() {
int num, sum = 0, divisor = 1;
printf("Please give a number: ");
scanf("%d", &num);
while (divisor < num) {
if (num % divisor == 0) {
sum += divisor;
}
divisor++;
}
if (sum == num) {
printf("Given %d is a perfect number.\n", num);
} else {
printf("Given %d is not a perfect number.\n", num);
}
return 0;
}
Output 1
Enter a number: 28 28 is a perfect number
Output 2
Enter a number: 43 43 is not a perfect number.
Program Explanation
- The program takes input for the number to be checked.
- It initializes a variable
sum
to 0 anddivisor
to 1. - It enters a while loop to iterate through possible divisors.
- Inside the loop, it checks if the number is divisible by the current divisor.
- If it is divisible, it adds the divisor to the sum.
- It increments the divisor by 1.
- The loop continues until the divisor is less than the given number.
- After the loop, it checks if the sum is equal to the given number.
- If it is, it prints that the number is perfect.
- Otherwise, it prints that the number is not perfect.
Conclusion
The program successfully determines whether a given number is perfect or not using a while loop. It checks for proper divisors of the number and calculates their sum. If the sum is equal to the given number, it is considered a perfect number. Otherwise, it is not. The program provides an efficient solution to identify perfect numbers