Solving Perfect Number Program in C

  • Thread starter Dili
  • Start date
In summary: Usually only 0 is used. The return statement is the last thing in the function, so if it's going to return a 0 or 1, nothing will happen after that. But if it were to return a different number, the function would terminate immediately.
  • #1
Dili
13
0
I need to write a program to check whether a number is perfect
eg: 6=1+2+3 , 28=1+2+4+7+14
i wrote one, but it doesn't work. also I've seen programs written with different preprocessors, we are required to write it on stdio.h and math.h


#include<stdio.h>
#include<math.h>

int main()
{
int a,b,c,d,i;
scanf("%d",&a);
{
for(i=1;i<a;i++)
b=a%i;
if(b==0)
{
c=0;
c=c+i;
}
if(c==a)
printf("perfect\n");
}
return 1;
}

 
Technology news on Phys.org
  • #2
Few mistakes in variable initialization and placing of parentheses. Here is the modified correct version. Make sure you understand the logic.

#include<stdio.h>
#include<math.h>

int main()
{
int a,b,c,d,i;
scanf("%d",&a);c=0;
for(i=1;i<a;i++)
{
b=a%i;
if(b==0)
{
c=c+i;
} }
if(c==a)
printf("perfect\n");
return 1;
}
 
  • #3
Heh. Here's a version you can't turn in, but which is far faster; it uses the Euclid identity of perfect numbers.

Code:
#include<stdio.h>
#include<math.h>

int is_prime(n) {
  int i,root;
  if (!(n&1))
    return n == 2;
  if (n%3 == 0)
    return n == 3;
  if (n < 25)
    return n > 1;
  root = (int)sqrt(n);
  for (i = 5; i <= root; i += 6)
    if (n%i == 0 || n%(i+2) == 0)
      return 0;
  return 1;
}

int main() {
  int even_part, odd_part;
  scanf("%d",&odd_part);
  even_part = 1;
  while (!(odd_part&3)) {
    odd_part = odd_part >> 2;
    even_part = even_part << 2;
  }
  if (!(odd_part&1)) {
    odd_part = odd_part >> 1;
    even_part = even_part << 1;
  }
  if (even_part << 1 == odd_part + 1 && is_prime(odd_part))
    printf("perfect\n");
  return 0;
}

Even faster and even cheatier, for 32-bit machines:

Code:
int main() {
  int n;
  scanf("%d",&n);
  if (n == 6 || n == 28 || n == 496 || n == 8128 || n == 33550336)
    printf("perfect\n");
  return 0;
}
 
Last edited:
  • #4
Thank you very much arunbg. I am not good at C programming.
I do now understand the placing of brackets and c=0.
If no bother can you explain what is the difference between return 1 and return 0
Ive used them but with no real understanding
Thanks again
 
  • #5
int main() {
int n;
scanf("%d",&n);
if (n == 6 || n == 28 || n == 496 || n == 8128 || n == 33550336 || n == 8589869056)
printf("perfect\n");
return 0;
}
Lol...nice one.
 
  • #6
Dili said:
If no bother can you explain what is the difference between return 1 and return 0
Ive used them but with no real understanding

Don't worry about it. These are error codes that the program returns, but they won't display or do anything unless they're checked by an outside program. 0 is supposed to mean "terminated normally" and anything other than 0 is supposed to mean "terminated with some kind of error".
 

1. What is a perfect number?

A perfect number is a positive integer that is equal to the sum of its proper divisors (factors excluding itself). For example, 6 is a perfect number because its proper divisors are 1, 2, and 3, and 1 + 2 + 3 = 6.

2. How do you solve a perfect number program in C?

To solve a perfect number program in C, you can use a loop to find the proper divisors of a given number and add them together. Then, you can check if the sum is equal to the original number to determine if it is a perfect number or not.

3. Can you provide an example of a perfect number program in C?

Yes, here is an example of a perfect number program in C:

#include <stdio.h>
int main() {
    int num, i, sum = 0;
    printf("Enter a positive integer: ");
    scanf("%d", &num);
    for (i = 1; i < num; i++) {
        if (num % i == 0) {
            sum += i;
        }
    }
    if (sum == num) {
        printf("%d is a perfect number.", num);
    } else {
        printf("%d is not a perfect number.", num);
    }
    return 0;
}

4. What is the time complexity of a perfect number program in C?

The time complexity of a perfect number program in C is O(n), where n is the given number. This is because the loop used to find the proper divisors runs n-1 times in the worst case scenario.

5. Can a negative number be a perfect number?

No, a negative number cannot be a perfect number. Perfect numbers are defined as positive integers, and negative numbers do not have proper divisors. Additionally, the loop used in a perfect number program in C would not work for negative numbers since it starts at 1 and only checks numbers up to the given number.

Similar threads

  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
4
Views
902
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
4
Views
736
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
3
Views
998
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
20
Views
2K
Back
Top