How To Ideas | How To Articles | How To Tutorials


0

How to check whether a number is Perfect in C


Perfect Numbers are those numbers which are equal to the sum of its proper positive divisors (excluding itself). Consider number 6, it has only four proper positive divisors 1, 2, 3 and 6. If we exclude 6, proper divisors left are 1, 2 and 3 and their sum is 6 which is equal to the number itself. So, 6 is a Perfect Number.

You can also say that Perfect Number is half the sum of all of its proper positive divisors. For 6, its proper divisors are 1, 2, 3 and 6 and their sum is 12. Now as 6 is half of this sum, 6 is a Perfect Number. You can learn more about Perfect Numbers on Wikipedia.

In this tutorial, I will show you a C language code to test whether a given number is Perfect Number or not.

Algorithm:

  1. Let a be the number which is to be checked.
  2. Let temp and sum be a temporary numbers initialized to 1 and 0 respectively.
  3. Now check whether temp is less than a or not. If not, move to step 7, otherwise step 4.
  4. Check if temp perfectly divides given number a or not. If yes move to next step, otherwise to step 6.
  5. Add value of temp to sum and store the result in sum itself.
  6. Increment the value of temp by 1 and move to step 3.
  7. Check whether value of variable sum is equal to given number or not. If yes, then given number is a Perfect Number, otherwise the given number is not a Perfect Number.

Instructions:

  1. We only need a single header file for this program to work.
    #include <stdio.h>
  2. Now ask the user to enter the number he/she wants to check.
    printf("Enter the numbers you want to check : ");
    scanf("%d", &num);
  3. Now call the function isPerfect with the given number as parameter and store the returned value in a temporary variable. Check if the value of temporary variable is 1 or not. If it is 1, then given number is Perfect Number otherwise not.
    Here isPerfect function is explained in next point and this function checks whether the argument is a Perfect Numberor not, if yes it returns 1, otherwise 0.

    temp = isPerfect(num);
    if(temp == 1)
        printf("%d is a Perfect Number...", num);
    else
        printf("%d is not a Perfect Number...", num);
  4. In isPerfect function, using two variables sum and temp and using a while loop (which will run until value of temp is less than given number), check whether temp perfectly divides given number, if it divides the number perfectly then add its value to sum variable and store result in sum itself. Also increment the value of temp in every iteration. After while loop check whether value of sum variable is equal to given number, if yes return 1, otherwise return 0.
    int isPerfect(int num)
    {
        int temp = 1, sum = 0;
        while(temp < num)
        {
            if(num % temp == 0)
                sum += temp;
            temp++;
        }
        if(num == sum)
            return 1;
        else
            return 0;
    }

Here are some screenshots of the running sample code.

Check Perfect Number using C

Check Perfect Number using C

Check Perfect Number using C

Check Perfect Number using C

Check Perfect Number using C

Check Perfect Number using C

You can also download the sample working code from here.

Filed in: C Tags: , , , , , , ,

Leave a Reply

Submit Comment



© 2013 How To Ideas. All rights reserved.
Proudly designed by Theme Junkie.