I am having trouble with a Recursive problem in C#

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hey guys. Basically, this hw problem requires in an input of 2 numbers and an output asfollows:
Intput: 4 9
Output:
From start of body: 4 9
From start of body: 3 9
From start of body: 2 9
From start of body: 1 9
From start of body: 0 9
From end of body: 1 9
From end of body: 2 9
From end of body: 3 9
From end of body: 4 9

I could swear that my code, atleast logically, should work, but I keep geting either an infinite loop, or only the "From start of body" half of the output to print. Heres my code:
Code:
 <span style="color:#008000; font-weight:bold using System<span style="color:#006400 ;<br/>
<br/>
    <span style="color:#ff0000 class RecursiveHomework<br/>
    <span style="color:#006400 {<br/>
        <span style="color:#a52a2a static <span style="color:#ff0000 void <span style="color:#191970; font-weight:bold Main<span style="color:#006400 ()<br/>
        <span style="color:#006400 {    <br/>
            <span style="color:#ff0000; font-weight:bold int num1<span style="color:#006400 , num2<span style="color:#006400 ;<br/>
            <br/>
            Console<span style="color:#006400 .<span style="color:#191970; font-weight:bold Write<span style="color:#006400 (<span style="color:#0000ff "Enter the first number:
 "<span style="color:#006400 );<br/>
            num1 = Convert<span style="color:#006400 .<span style="color:#191970; font-weight:bold ToInt32<span style="color:#006400 (Console<span style="color:#006400 .<span style="color:#191970; font-weight:bold ReadLine<span style="color:#006400 ());<br/>
            Console<span style="color:#006400 .<span style="color:#191970; font-weight:bold Write<span style="color:#006400 (<span style="color:#0000ff "Enter the second number:
 "<span style="color:#006400 );<br/>
            num2 = Convert<span style="color:#006400 .<span style="color:#191970; font-weight:bold ToInt32<span style="color:#006400 (Console<span style="color:#006400 .<span style="color:#191970; font-weight:bold ReadLine<span style="color:#006400 ());<br/>
            Console<span style="color:#006400 .<span style="color:#191970; font-weight:bold WriteLine<span style="color:#006400 ();<br/>
         <br/>
            <span style="color:#ff0000; font-weight:bold int sum = num1 <span style="color:#006400 + num2<span style="color:#006400 ;<br/>
            <br/>
            RecursiveHomework Recursion = <span style="color:#008b8b; font-weight:bold new <span style="color:#191970; font-weight:bold RecursiveHomework<span style="color:#006400 ();<br/>
            Recursion<span style="color:#006400 .<span style="color:#191970; font-weight:bold RecSum<span style="color:#006400 (n 
<div id="x_:6d um1<span style="color:#006400 ,num2<span style="color:#006400 );<br/>
            <br/>
            <br/>
            Console<span style="color:#006400 .<span style="color:#191970; font-weight:bold WriteLine<span style="color:#006400 (<span style="color:#0000ff "The sum of " <span style="color:#006400 + num1 <span style="color:#006400 + <span style="color:#0000ff "
 and " <span style="color:#006400 + num2 <span style="color:#006400 + <span style="color:#0000ff " is " <span style="color:#006400 + sum<span style="color:#006400 );<br/>
            <br/>
            Console<span style="color:#006400 .<span style="color:#191970; font-weight:bold ReadLine<span style="color:#006400 ();<br/>
        <span style="color:#006400 }<br/>
        <br/>
         <span style="color:#0000ff; font-weight:bold public <span style="color:#ff0000; font-weight:bold int <span style="color:#191970; font-weight:bold RecSum<span style="color:#006400 (<span style="color:#ff0000; font-weight:bold int a<span style="color:#006400 , <span style="color:#ff0000; font-weight:bold int b<span style="color:#006400 )<br/>
        <span style="color:#006400 {<br/>
             <span style="color:#ff0000; font-weight:bold int a2 = a<span style="color:#006400 ;<br/>
             <span style="color:#ff0000; font-weight:bold int a3 = a<span style="color:#006400 ;<br/>
<br/>
             <span style="color:#0000ff; font-weight:bold if <span style="color:#006400 (a3==<span style="color:#00008b 0<span style="color:#006400 )<br/>
             <span style="color:#006400 {<br/>
                 <span style="color:#0000ff; font-weight:bold while<span style="color:#006400 (a<span style="color:#006400 <=a2<span style="color:#006400 )<br/>
                 <span style="color:#006400 {<br/>
                     Console<span style="color:#006400 .<span style="color:#191970; font-weight:bold WriteLine<span style="color:#006400 (<span style="color:#0000ff "From
 End of body: " <span style="color:#006400 + a <span style="color:#006400 + <span style="color:#0000ff " " <span style="color:#006400 + b<span style="color:#006400 );<br/>
                     <span style="color:#000080 return <span style="color:#191970; font-weight:bold RecSum<span style="color:#006400 (a<span style="color:#006400 +<span style="color:#00008b 1<span style="color:#006400 , b<span style="color:#006400 );    <br/>
                 <span style="color:#006400 }<br/>
                 <br/>
                     <span style="color:#000080 return <span style="color:#00008b 0<span style="color:#006400 ;<br/>
            <span style="color:#006400 }<br/>
             <br/>
             <span style="color:#0000ff; font-weight:bold else <span style="color:#0000ff; font-weight:bold if<span style="color:#006400 (a<span style="color:#006400 ><span style="color:#00008b 10<span style="color:#006400 )<br/>
                 <span style="color:#006400 {<br/>
                     Console<span style="color:#006400 .<span style="color:#191970; font-weight:bold WriteLine<span style="color:#006400 (<span style="color:#0000ff "From
 Start of body: " <span style="color:#006400 + a <span style="color:#006400 + <span style="color:#0000ff " " <span style="color:#006400 + b<span style="color:#006400 );<br/>
                     a3<span style="color:#006400 --;<br/>
                     <span style="color:#000080 return <span style="color:#191970; font-weight:bold RecSum<span style="color:#006400 (a<span style="color:#006400 -<span style="color:#00008b 1<span style="color:#006400 , b<span style="color:#006400 );<br/>
                 <span style="color:#006400 }<br/>
             <br/>
             <span style="color:#0000ff; font-weight:bold else<br/>
             <span style="color:#006400 {<br/>
                 <span style="color:#000080 return <span style="color:#00008b 0<span style="color:#006400 ;<br/>
             <span style="color:#006400 }<br/>
             <br/>
         <span style="color:#006400 }<br/>
         <br/>
         <br/>
         <br/>
    <span style="color:#006400 } 
 <span style="color:#006400
<br/>



View the full article
 
Back
Top