Click here to Skip to main content
16,012,107 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Im doing some online course, I'm trying to learn C # Smile | :) . It appears to me that i created infinite loop. Any hepl?
C#
 class Program
    {
        /// <summary>
        /// Calculates the sum of the integers between start and end (including start and end)
        /// [TestMethod]
        /// </summary>
        /// <param name="start">Start number</param>
        /// <param name="end">End number</param>
        /// <returns>Sum of the integers between start and end (including start and end)</returns>
        public static int Sum(int start, int end)
        {
            
            if (start >= end)
            {
                return  0;
            }
            // TODO add your code here
            else
            {
               
                return Sum(start, end);
            }
 
        }
 
        static void Main(string[] args)
        {
            int start, end;
            Console.WriteLine("ukucajte prvi broj");
            start =Convert.ToInt32(Console.ReadLine());
 
            Console.WriteLine("ukucajte drugi broj");
            end = Convert.ToInt32(Console.ReadLine());
 
           
            Console.WriteLine("suma je: " + Sum(start,end));
       
            Console.Read();
        }
    }
}


What I have tried:

this is a error:
An unhandled exception of type 'System.StackOverflowException' occurred in Methods.exe
Posted
Updated 30-Jul-16 0:35am
Comments
Richard MacCutchan 30-Jul-16 6:39am    
You already posted this in the C# forum - please post in one forum only.

Well, yes...
C#
public static int Sum(int start, int end)
{
    if (start >= end)
    {
        return  0;
    }
    // TODO add your code here
    else
    {
        return Sum(start, end);
    }
}

If you call Sum with start less than end:
C#
Sum(1, 2);
Then it will look at the two values, and see which is the greater.
It's end, so the if test will fail, and it will execute the else clause ... which passes the same values to the Sum method so it does teh same thing again, and again, until it runs out of stack space and fails!
Probably, what you meant to do was
C#
return start + Sum(start + 1, end);
But to be honest, a recursive method to do this is a bit overkill! :laugh:
Why not try it with a simple for loop?
 
Share this answer
 
try using simple for loop

C#
public static int Sum(int start, int end)
   {

       if (start >= end)
       {
           return 0;
       }
       else
       {
           int value = 0;
           for (int i = start; i <= end; i++)
               value += i;

           return value;
       }

   }


using recursion[^]

C#
public static int Sum(int start, int end)
    {

        if (start > end)
        {
            return 0;
        }
        else
        { 
            return  start  + Sum(start+1 , end);
        }

    }
 
Share this answer
 
v2
Comments
Member 11383935 30-Jul-16 7:05am    
Yes, this is a working solution. Can you do the same thing but without FOR loop?
Karthik_Mahalingam 30-Jul-16 7:37am    
check updated solution.
Member 11383935 30-Jul-16 7:09am    
I'm trying to learn and cours still have not got to the FOR loop.
Member 11383935 30-Jul-16 8:12am    
Thanks.
Karthik_Mahalingam 30-Jul-16 8:33am    
welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900