program to find perfect number : error in output . perfect number is number whose sum of factors equals the given number

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

int main()
{
 int rem, num, i, sum;

 sum=0;
 num=28;

 for(i=1;i<num;i++)
 {
     if(num%i==0)
     {
         rem=num%i;
         sum=sum+rem;
     }
 }
 
 if(sum==num)
 {  
    printf("perfect number");
 }
 else
    printf("not perfect");
}
Sahadat Hossain

why are you doing this, you need to get the total sum of all the proper divisors (proper divisor of a natural number is the divisor that is strictly less than the number) of num, but what you are doing is adding rem = num%i, which is basically always 0 as num is divisible by i what you checked in your if, so what you are doing is not making any sense

if(num%i==0)
{
    rem=num%i;  // here `rem` is always `0` as `num%i == 0`
    sum=sum+rem;  
}

rather your logic should be like below, as the divisor is i so you should add all the divisors (representing by the i when num is divisible by i) in your sum

if(num%i==0)
{
    sum = sum + i;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Program to find if a number is perfect

From Dev

Perfect number from input

From Dev

Optimized way to find if a number is a perfect square

From Dev

Find numbers of subarray of an array whose sum is divided by given number

From Dev

Find permutations of a given set of numbers whose sum APPROACH a target number

From Dev

Find Pair Of Integers in Array whose Sum is Given Number in PHP

From Dev

Given a number N, can N be expressed as the sum of two or more consecutive perfect squares?

From Dev

Find elements that sum to a given number

From Dev

Calculate number of perfect squares in a given big range in C

From Dev

Algorithm for 2 player game subtracting perfect squares from a given number

From Dev

Calculate number of perfect squares in a given big range in C

From Dev

How to find out the best combination of a given vector whose sum is closest to a given number

From Dev

Minimal perfect hash for N number of unknown keys

From Dev

Efficient perfect matching algorithm for even number of vertices?

From Dev

Get closest number to perfect square in batch

From Dev

Can't check whether a number is a perfect or not

From Dev

Count even,odd and perfect number for arrays java?

From Dev

Optimize a perfect number check to O(sqrt(n))

From Dev

how to find the minimum number of primatics that sum to a given number

From Dev

Finding the sum of a given number

From Dev

sum of BITS is given find thar number

From Dev

how to find the output for a file if head number and tail number is given

From Dev

Most efficient way to count number of pairs in an array whose product is a perfect square?

From Dev

how to find the length of the longest contiguous subarray whose sum is divisible by a given number

From Dev

what can be the best algorithm to find numbers with only single set bit and whose sum is equal to given number?

From Dev

How to find the factors of a number with for loops?

From Dev

Number of Subarray whose sum greater than given value

From Dev

Sum to a number program in Prolog

From Dev

Sum to a number program in Prolog

Related Related

  1. 1

    Program to find if a number is perfect

  2. 2

    Perfect number from input

  3. 3

    Optimized way to find if a number is a perfect square

  4. 4

    Find numbers of subarray of an array whose sum is divided by given number

  5. 5

    Find permutations of a given set of numbers whose sum APPROACH a target number

  6. 6

    Find Pair Of Integers in Array whose Sum is Given Number in PHP

  7. 7

    Given a number N, can N be expressed as the sum of two or more consecutive perfect squares?

  8. 8

    Find elements that sum to a given number

  9. 9

    Calculate number of perfect squares in a given big range in C

  10. 10

    Algorithm for 2 player game subtracting perfect squares from a given number

  11. 11

    Calculate number of perfect squares in a given big range in C

  12. 12

    How to find out the best combination of a given vector whose sum is closest to a given number

  13. 13

    Minimal perfect hash for N number of unknown keys

  14. 14

    Efficient perfect matching algorithm for even number of vertices?

  15. 15

    Get closest number to perfect square in batch

  16. 16

    Can't check whether a number is a perfect or not

  17. 17

    Count even,odd and perfect number for arrays java?

  18. 18

    Optimize a perfect number check to O(sqrt(n))

  19. 19

    how to find the minimum number of primatics that sum to a given number

  20. 20

    Finding the sum of a given number

  21. 21

    sum of BITS is given find thar number

  22. 22

    how to find the output for a file if head number and tail number is given

  23. 23

    Most efficient way to count number of pairs in an array whose product is a perfect square?

  24. 24

    how to find the length of the longest contiguous subarray whose sum is divisible by a given number

  25. 25

    what can be the best algorithm to find numbers with only single set bit and whose sum is equal to given number?

  26. 26

    How to find the factors of a number with for loops?

  27. 27

    Number of Subarray whose sum greater than given value

  28. 28

    Sum to a number program in Prolog

  29. 29

    Sum to a number program in Prolog

HotTag

Archive