top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Write a C program to check perfect number?

+1 vote
817 views
Write a C program to check perfect number?
posted Nov 11, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

3 Answers

+1 vote

Hi try this function to check a number is perfect or not:

 bool CheckPrimeNumber(int number)
    {
        int  rem, sum = 0;
        for (int i = 1; i <=(int)(sqrt( (number )); i++)
        {
            if(! number % i)
            {
                sum = sum + i+number/i;
            }
        }
        if (sum == number)
            return true;
        else
            return false;
    }

Time Complexity : O(sqrt(n))

answer Nov 15, 2016 by Shivam Kumar Pandey
0 votes

Perfect number, a positive integer that is equal to the sum of its proper divisors. The smallest perfect number is 6, which is the sum of 1, 2, and 3. Other perfect numbers are 28, 496, and 8,128.

#include<stdio.h>
void main()
{
int n,i,sum=0;
printf("Enter the number:");
scanf("%d",&n);

while(i<n)
{
    if(n%i==0)
           sum=sum+i;
          i++;
  }
  if(sum==n)
      printf("%d is a perfect number",i);
  else
      printf("%d is not a perfect number",i);

  return 0;
}
answer Nov 11, 2016 by Abhishek Maheshwari
So many errors in the program,
0 votes
#include <stdio.h>

int main()
{
    int number, rem, sum = 0, i;

    printf("Enter a Number\n");
    scanf("%d", &number);
    for (i = 1; i <= (number - 1); i++)
    {
        rem = number % i;
    if (rem == 0)
        {
            sum = sum + i;
        }
    }
    if (sum == number)
        printf("Entered Number is perfect number");
    else
        printf("Entered Number is not a perfect number");
    return 0;
}
answer Nov 11, 2016 by Chirag Gangdev
Is O(n) a good approach for perfect number?
Similar Questions
–1 vote

Write a C program to check if the given string is repeated substring or not.
ex
1: abcabcabc - yes (as abc is repeated.)
2: abcdabababababab - no

+3 votes

Decimal to Factorial
23(D) = 3210(F)

23 / 3! = 3 (5)
5 / 2! = 2 (1)
1 / 1! = 1 (0)
0 / 0! = 0 (0)

Factorial to Decimal
3210(F)=23(D),
3*3!+2*2!+1*1!+0*0! = 23(D)

...