Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7699217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:25:38+00:00 2026-05-31T22:25:38+00:00

Having trouble with recursive methods in c#. When compiled it should just display the

  • 0

Having trouble with recursive methods in c#. When compiled it should just display the total sumUpToo of all number for the given int, i.e
– input 10
– output 55 (10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0)

Im not able to find any info anywhere so if someone has a link to a website that can teach me how to go through it, would be greatly appreciated.

class Program
{
    static void Main(string[] args)
    {
        public static int sum(int x)
        {
        Console.WriteLine("num");
        x = Console.ReadLine();
        int sum = 0, i = 0;
        for (i = 0; i <= x; i++)

        {
            sum = sum + i;
        }
        Console.WriteLine("{0}", sum);
        }
        public static int recursivesum(int x)
        {
        if (x <= 1)
            Console.WriteLine("{0}", x);
        else
           (x + Recursivesum(x - 1));
        }

edit * This is the adjustment seems to be working fine now, if im not mistaken. Thanks for all the help

class Program
{
    static void Main(string[] args)
    {
        int x;
        Console.Write("Please enter an integer value to sum up to: ");
        x = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("The sum of all numbers up to and including {0} is {1}",x, recursivesum(x));
    }

    public static int sum(int x)
    {
        int sum = 0, i = 0;
        for (i = 0; i <= x; i++)
        {
        sum = sum + i;
        }
        return sum;
    }

    public static int recursivesum(int x)
    {
        if (x <= 1)
            return x;    
        else
            return x + recursivesum(x-1);

    }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:25:39+00:00Added an answer on May 31, 2026 at 10:25 pm

    A recursive function is a function that calls it self. You need a base case that will exit the function and a recursive case where the function calls itself with modified parameters. Your function should look something like this:

    public static int recursivesum(int x)
    {
        if (x <= 1)
            return x;    // this is the base case
        else
            return x + recursivesum(x-1);
    }
    

    So to use this function, you simple call it like this:

    recursivesum(10);
    

    If you follow the logic of the function you will see this will return 10 + recursivesum(9). recursivesum(9) will return 9 + recursivesum(8). So now we have 10 + 9 + recursivesum(8).

    This will carry on until we reach the point where we have 10+9+8+7+6+5+4+3+2+recursivesum(1). Now if you look at the function again, recursivesum(1) doesn’t call itself again. Instead it just returns x. So now the function will unwind and you’ll get the result you expect.

    One final note on recursion. Recursion can be a wonderful elegant way to implement some algorithms, but it has it’s dangers. This site isn’t called stack overflow for nothing!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: I am having trouble implementing a recursive image lazy load in all relevant
Hey I'm having some trouble understanding how Recursive Algebraic Types work and how to
I'm having trouble finding a simple statement to skip the duplicates for this recursive
I'm writing a recursive function in JS and having some trouble. Let's start with
I'm having trouble getting my head around a problem on recursive nested lists. The
I have a recursive function with a bug that I am having trouble with.
Having trouble with proper regex for RewriteCond RewriteCond %{REQUEST_URI} !^/foo/ Works as expected, that
Having trouble with the following segment of code. I'm getting a parameter count mismatch.
Having Trouble with Entity Framework. I have been populating EntityReferences with an EntityKey inorder
Having trouble finding an explanation in the docs, would someone be kind enough to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.