dimanche 7 octobre 2018

Find sum of factors of given integer and divide it by the given integer

I'm new to C language and I could really use help with this question:

The abundancy of an integer is defined as the perfect divisors of a number (factors not including the number itself) divided by the number itself. For example, the abundance of 8 is (1+2+4)/8 = 7/8. Write a C function which takes a single integer as input and returns the abundance of that number.

This is as far as I got. I can compile it but I keep getting the incorrect answer. Please help and thanks in advance

#include <stdio.h>
int main()
{
        int number, i, sum, abundancy;
        sum==0;
        abundancy==0;

        printf("Enter an integer:");
        scanf("%d", &number);

        for(i=1; i<=number; i++)
        {
                if (i!=number)
                {
                        if (number%i == 0)
                        {
                                sum+=i;
                                        {
                                                abundancy=sum/number;
                                                printf("The abundancy is %d", abundancy);
                                        }
                        }
        }
        }
        return 0;
}

. This is as far as I got on an online compiler This is what I get when I compile it

Aucun commentaire:

Enregistrer un commentaire