## C Program To Check A Given Number Is Perfect Or Not Using While Loop

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

1. Take input for the number to be checked from the user.
2. Initialize a variable `sum` to 0, which will store the sum of the proper divisors of the number.
3. Initialize a variable divisor to 1, which will be used to check for divisors.
4. Enter a while loop with the condition divisor < number to iterate through possible divisors.
5. Inside the while loop, check if number is divisible by divisor.
6. If number is divisible, add the value of divisor to sum.
7. Increment the value of divisor by 1 to move on to the next possible divisor.
8. The while loop continues until divisor is less than the given number.
9. After the while loop, check if sum is equal to the given number.
10. If sum is equal to number, print that the given number is a perfect number.
11. 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;
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 and `divisor` 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