简体   繁体   中英

Recursive method to add odd numbers

I have the below snippet of code to use a recursive method to add the sum of odd numbers. I have already coded the iterative method successfully that adds the sum of all odd numbers between n and m which are entered by the user. I'd like to reach that goal but am started slow to make sure I understand what is happening. I know that it makes more sense to do it iteratively, however I am experimenting with the two types to see which is more efficient. I am stuck on the below as it is not doing what i want it to and i can't understand why.

import java.util.*;

public class SumofOdd
{
    public static void main (String [] args)
    {
    int n = 0;
    Scanner sc = new Scanner(System.in);
    System.out.println("Please enter an odd number");
    n = sc.nextInt();
    int x = add(n);
}

public static int add(int x)
{
    if (x == 0)
    {
        return 0;
    }
    else
    {
        return (x + add(x-1));
    }
}
}

I have changed the above to the below. It compiles however stops after I enter the number. import java.util.*;

public class SumofOdd
{
    public static void main (String [] args)
    {
    int n = 0;
    Scanner sc = new Scanner(System.in);
    System.out.println("Please enter an odd number");
    n = sc.nextInt();

    if (n%2 == 0)
    {
        System.out.println("The number entered is even");
    }
    else
    {
        int x = add(n);
    }
}

public static int add(int x)
{
    if (x <= 0)
    {
        return 0;
    }
    else
    {
        return (x + add(x-2));
    }
}
}
import java.util.*;
public class OddR{

    public static void main (String Args [])
    {
        Scanner s = new Scanner(System.in);
        System.out.println("Enter an odd number");
        int max = s.nextInt();
        if((max% 2) == 0) {
            System.out.println(max + " is Even number and therefore is invalid");
        }
        else{
            System.out.println("Enter a greater odd number");
            int m = s.nextInt();
            if (m <max){
                System.out.println("Invalid data");

            }
            else{
                if((m % 2) == 0) {
                    System.out.println(m + " is Even number and therefore is invalid");

                }
                else{

                    int data =  (addodd(m)- addodd(max))+max;
                    System.out.print("sum:"+data);
                }
            }
        }
    }

    public static int addodd(int m)
    {

        if(m<=0)
        {
            return 0;    
        }

        if(m%2 != 0)
        {
            return (m+addodd(m-1));
        }
        else
        {
            return addodd(m-1);
        }
    }
}

This is the answer recursively of the sum of odd numbers from n to m

public int addOdds(int n) {
  if (n <= 0) {
    return 0;
  }
  if (n % 2 == 0) {
    return addOdds(n - 1);  
  }
  return x + addOdds(n - 1);
}

Take care, I never tested the code.

class Oddsum {
    

   public int addodd(int n)
    {
     if(n<=0)
     {
       return 0;    
     }
     if(n%2 != 0)
     {
       return (n+addodd(n-1));
     }
     else
     {
       return addodd(n-1);
     }
   }
}

public class Xyz {

    public static void main (String[] v)
    {
      int n = 9;
      Oddsum o = new Oddsum();
      int data =  o.addodd(n);
      System.out.print("sum:"+data);
    }
}

This is working fine

public static void main (String[] args){
        
       public static int oddSum(int s){
       if (s <= 0)
           return 0;
      
        else
            return s + oddSum(s -2);
       
         }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM