I have a program that prompts the user to enter up to 10 digits. Then the program uses the function to return the sum of the divisors of each minus number entered and displays it until that number is 1 or 0. My problem is that the function stops after adding 45 divisors. I am trying to use recursion here because the function will be called "n" the number of times until each of them is 0 or 1. What makes recursion so useful in such situations and how can it be applied here? Is there something that I am missing in the name of this function? Can anybody help me?
For example,
if the user enters: 25 -4 6 45 (then press the enter button)
the program should output:
25 1 0
-4 0 0
6 6
45 33 15 9 4 3 1 0 0
6 is an example of a perfect number and will be repeated, therefore, if an ideal number occurs, it should stop the summation of the divisors. When the sum is 0, it should print once and then stop. Also -4 is out of range, so it should also print 0. Must be greater than 1.
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int i=0, j=0, k=0, l=0, num = 0, x = 0, count = 0, total = 0, z = 0;
signed int b[11] = {0};
char discard;
printf( "\n\nPlease enter your list of numbers: " );
while(i < 11 && (scanf("%d%1[^\n]s", &b[i], &discard)) == 2)
{
++count;
i++;
}
puts("");
puts("");
while(k <= count)
{
x=b[k];
num = sum_divisors(x);
printf("%d " , num);
k++;
puts("");
}
}
int sum_divisors(int a)
{
int total = 0;
int z = 0;
printf("%d ", a);
if(a < 1)
{
total = 0;
}else
{
if((a%1 == 0) && a !=1)
total = total + 1;
for(z=2; z<102; z++)
{
if((a%z == 0) && a != z)
{
total = total + z;
}
}
}
return total;
}
source
share