Home > OS >  Printing the number with highest sum of devisors
Printing the number with highest sum of devisors

Time:11-09

i have a homework but i cant get the answer I need to write a program in C... Here is what is needed: You need to enter "n" natural number as input , and from all the natural numbers smaller than "n" , its needed to print the number which has the highest sum of devisors. For exp: INPUT 10 , OUTPUT 8 Can anyone help me somehow? I would really appreciate it !

i tried writing a program for finding the devisor of a number but i cant get far from here

#include <stdio.h>
int main() {
    int x, i;
    printf("\nInput an integer: ");
    scanf("%d", &x);
    printf("All the divisor of %d are: ", x);
    for(i = 1; i < x; i  ) {
        if((x%i) == 0){
            printf("\n%d", i);
        
        }
    }
}

CodePudding user response:

I have implemented using function which will takes input number from user and then return the sum of divisor. hope this is one you looking for

/* function to return of sum of divisor
** input: x: integer number from user input
** return sum: sum of divisor of x 
*/

int sum_of_divisor(int x)
{
    int sum = 0;
    for(int i = 1; i < x; i  ) 
    {
        
        if((x%i) == 0)
        {
            printf("%d\n", i);
            sum = sum i;
        
        }
    }
    return sum;
}

int main() {
    int x, i;
    printf("\nInput an integer: ");
    scanf("%d", &x);
    printf("All the divisor of %d are: ", x);
    printf("the sum of divisor is %d ", sum_of_divisor(x));
    return 0;
}

Output:

Input an integer: 10
All the divisor of 10 are: 1
2
5
the sum of divisor is 8 

CodePudding user response:

After checking if i is a divisor of x, you should then store that value in another variable, for example m.

Repeat until a new divisor i is higher than that number. Add this new value to m.

  •  Tags:  
  • c
  • Related