   # Write a C program to check perfect number?

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

+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

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
So many errors in the program,
``````#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
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