C Program To Find Perfect Number using For loop


Lets write a C program to check if user entered number is a perfect number or not, using for loop.

Related Read:
Basic Arithmetic Operations In C
For Loop In C Programming Language
C Program to Find Factors of a Number using For Loop
C Program to Find Perfect Number using while loop

Perfect Number: A number is called perfect number if sum of its divisors(except the number itself) is equal to the number.

For Example: If the user entered number is 28. The numbers which perfectly divide 28 are 1, 2, 4, 7, 14, 28. Leave 28 and add all other numbers. i.e., 1 + 2 + 4 + 7 + 14 = 28. So the entered number and the sum are equal. So 28 is a perfect number.

Video Tutorial: C Program To Check Perfect Number or Not, using For loop


[youtube https://www.youtube.com/watch?v=i1bAVHYOOyU]

YouTube Link: https://www.youtube.com/watch?v=i1bAVHYOOyU [Watch the Video In Full Screen.]

Source Code: C Program To Find Perfect Number using For loop

#include<stdio.h>

int main()
{
    int num, count, sum = 0;

    printf("Enter a number\n");
    scanf("%d", &num);

    printf("Factors of %d are(except the number itself):\n", num);
    for(count = 1; count < num; count++)
    {
        if(num % count == 0)
        {
            printf("%d\n", count);
            sum = sum + count;
        }
    }

    if(sum == num)
        printf("\n%d is a perfect number\n", num);
    else
        printf("\n%d is not a perfect number\n", num);

    return 0;
}

Output 1
Enter a number
6
Factors of 6 are(except the number itself):
1
2
3

6 is a perfect number

Output 2
Enter a number
8
Factors of 8 are(except the number itself):
1
2
4

8 is not a perfect number

Output 3
Enter a number
14
Factors of 14 are(except the number itself):
1
2
7

14 is not a perfect number

Output 4
Enter a number
28
Factors of 28 are(except the number itself):
1
2
4
7
14

28 is a perfect number

Logic To Check Perfect Number or Not using For loop

We ask the user to enter a number and store it inside address of integer variable num. Next we find factors of that user entered number i.e., all the numbers which perfectly divide the user entered number. In that factors list, we exclude the user entered number itself.

Now we add all the factors(except the user entered number itself). If the sum and the user entered number are equal then its a perfect number.

We initialize for loop counter variable count to 1 and iterate the for loop until count is less than num. For each iteration of for loop we increment the value of count by 1.

Inside for loop we check if num % count == 0. If true, we add value of count to previous value of variable sum.

After control exits for loop we check if the value of sum and number entered by the user are same. If its same, then the user entered number is perfect number. If not, the number is not a perfect number.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

Your email address will not be published. Required fields are marked *