a program in c with detail explanation  to check if a given number is a perfect number or not using a do while loop.

Using do While Loop
In this approach, we’ll just use a do while loop to iterate through all the integers from 1 to the number and see if the number is a divisor of the number.
In this case, we simply use a do while loop to iterate over all the integers from 1 to number and check if the number is a divisor of the number.
Examples:
Input: 28
Divisors of 28 are 1, 2, 4, 7 and 14.
Sum = 1 + 2 + 4 + 7 + 14 = 28
Output: 28 is a perfect number

Input: 34
Divisors of 34 are 1, 2 and 17.
Sum = 1 + 2 + 17 = 20. 34 != 20
Output: 34 is not a perfect number
Here is source code of the C Program to check whether a given number is perfect number or not using do while loop.


C Program to check whether a given number is perfect number or not using do while loop.

						

/* * program in C language using do while loop to check whether a given number is a perfect number or not */ #include <stdio.h> int main(void) { int number,i,sum=0; printf("\nEnter the number:\n "); scanf("%d", &number); i = 1; do{ if(number % i == 0) sum += i; i++; }while(i<number); if(sum==number) printf("\n%d is a perfect number",number); else printf("%d is not a perfect number",number); }

Program Explanation
1. Accept the number as input from user and store the number in a number variable.
2. Go through all the numbers from 1 to number and see if the number is divisible by the number.
3. In the while loop, take the number, divide by i and store the result in variable sum.
4. If the sum is equals the number then print "The number is perfect number."
5. Otherwise show or print "number is not perfect".


Previous :-->> 16.Write a program in c to print the pattern of a hollow square using a while loop.
 -->> NEXT: 18.Write a program in c to calculate the sum of all prime numbers between two given numbers using a for loop.
-->>ALL Loops Assignments in c