Luke Hetherington
Luke Hetherington

Reputation: 37

SUM Recursive function in C

I'm trying to recursive function in C that calculates the sum of the numbers from x to max (inclusive). For example, sum (4, 7) would compute 4 + 5 + 6 + 7 and return the value 22. The function code a must be recursive so you are not allowed to use any conventional loop constructs.

I have this and i think it should work but i'm not entirely sure why its not

#include <stdio.h>

int main()
{
int sum (x, max);
   int total, y, x, max;
   if (x<max){
       y=x+1;
       total = x+sum(y,max);
       return total;
    return x;
   }


return 0;
}

Thanks for any help with this in advance!

Upvotes: 1

Views: 1262

Answers (3)

Bicolano
Bicolano

Reputation: 90

I spotted some errors on your code. I'm not a pro yet but here's what I think

I just edit your code. removed, added and rearranged some stuff*/

/*First, let's look at your code*/
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int sum(x, max);//I think what you want to do here is declare a function but instead declaring, you define it here because you added semicolon (;) 
    int total, x, y, max;

    if(x < max)
    {
        y = x + 1;
        total = x + sum(y, max); //you don't have a function declaration for "sum"

        return total;

        return x; //this will never return since you already "return the total before this"
    }

    return 0;
}


//////////////////////////////////////////////////////////////

/*And I think this is what you want to do*/
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int x = 4, max = 6; //We declare this here for the "sum" function. This way "sum" function can recognize these variables
    int total = x; //be sure to set the total to x.

    //you can make a void function for this instead of "int". but either way, it can do the job.
    void sum(int y) //no need to pass a "max" because "max" is already recognized by the "sum" function since we declare the variables at the top of "main" function
    {
        if(x < max)//don't make it x <= max. Because of the argument "total = total + (x + 1)" on this function. If you do, the total will exceed.
        {
            //You can see here why we set the value of "total" to x.
            total = total + (x + 1);//And also, you can see why we didn't make the argument in if() statement like this: if(x <= max).
            x++;//increment "x" every loop

            //call the function again and pass the total until x == max.
            sum(total);
        }
    }

    //pass the x
    sum(x);

    //check the answer
    printf("The total is %d\n\n", total);

    return 0;
}

//////////////////////////////////////////////////////////////

/*It looks messy with comments*/
/*Here's the code looks like without a comment.It's pretty short code if you look remove the comments.. LOL..*/
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int x = 4, max = 6;
    int total = x;

    void sum(int y) 
    {
        if(x < max)
        {
            total = total + (x + 1);
            x++;

            sum(total);
        }
    }

    sum(x);

    //check the answer
    printf("The total is %d\n\n", total);

    return 0;
}

Upvotes: 0

Dipika Walanjkar
Dipika Walanjkar

Reputation: 29

    #include<stdio.h>
#include<stdlib.h>
#include<string.h>

int sum(int s,int max)
{
    if(s==max)
    {
        return s;
    }
    else
    {       
        return(s+sum(s+1,max));
    }
}
int main()
{
    int r,s,max;
    printf("\n enter s and max");
    scanf("%d%d",&s,&max);
    r=sum(s,max);
    printf("%d",r);
}

Upvotes: 1

Anderson Green
Anderson Green

Reputation: 31810

Here is one possible solution:

#include <stdio.h>

int sum_in_range(int a, int b){
    if(a != b){
        return sum_in_range(a+1,b)+a;
    }
    else{
        return b;
    }
}

int main(void) {
    // your code goes here
    printf("%d",sum_in_range(2,4));
    return 0;
}

Upvotes: 1

Related Questions