Question

Using Java IDE Write a recursive method which takes an integer number and returns the sum...

Using Java IDE

Write a recursive method which takes an integer number and returns the sum of the numbers from 1 to that number. The method must solve the problem recursively.Then write an application which calls the method with a few different numbers and displays the return value of the method.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//SumN.java
import java.util.Scanner;

public class SumN {
    public static int sumofn(int num) {
        if(num == 0){
            return 0;
        }
        else{
            return num + sumofn(num-1);
        }
    }

    public static void main(String args[]) {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a positive integer: ");
        int num = scan.nextInt();
        System.out.println("Sum1 till "+num +" is "+sumofn(num));
    }
}
Add a comment
Know the answer?
Add Answer to:
Using Java IDE Write a recursive method which takes an integer number and returns the sum...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT