How to use a function recursively?

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)
  {

    //Fields
    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;


    //Prompt message
    printf( "\n\nPlease enter your list of numbers: " );


    //This while loop scans until the enter button is pressed
    while(i < 11 && (scanf("%d%1[^\n]s", &b[i], &discard)) == 2)
    {
      ++count;
      i++;
    } 
    puts("");
    puts("");
    //Display Factors 

    while(k <= count)
    { 
      x=b[k];
      num = sum_divisors(x);
      printf("%d " , num);
      k++;  
      puts("");
    }


   }//End of main

    //function to sum the divisors together
    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;
           }//if
         }//for
      }//end if/else statement
      //  printf("%d ", total); 
      return total;
    }//end function sum_divisors
+4
source share
1 answer

-, , , a % 1 == 0 a, , magic, 102 for sum_divisors; , 102, , , 102 .

, , sum_divisors, , , , , , .

  • , , sum_divisors (, , , ), void .
  • (a%1==0)&&(a!=1) .

      if( a <= 1 )
          return;
    

    ,

  • , for , . 102 a-1, a a!=z
  • ,
  • , sum_divisors( total );

- , total = 1, a=1 .

, , ,

void sum_divisors( int a ) 
{
    int total = 1;     // divisor sum of a
    int z;

    printf("%d ", a);  // print value

    if( a <= 1 )       // base case
        return;

    for( z = 2; z < a-1; ++z)  // z < a-1 as to not include a in total
    {    
        if( a%z == 0 )
        {
            total += z;
        }
    }   
    if( total == a )   // a is a perfect number so we're done
        return;

    sum_divisors( total ); // call sum_divisors recursively with a = total
}

,

+1

Source: https://habr.com/ru/post/1613922/


All Articles