Question

Write a complete Java program to recursively find the SUM from 0 to N for a...

Write a complete Java program to recursively find the SUM from 0 to N for a non-negative integer N.

Also, design an exception class to handle the negative number entered by the user.

Homework Answers

Answer #1

Java code pasted below.

import java.util.Scanner;
public class AddN {
public static void main(String[] args) {
//scanner object for reading the number
Scanner sc=new Scanner(System.in);   
System.out.println("Enter N:");
int number=sc.nextInt();
int sum;
try{
//calling recursive function
sum = addN(number);
System.out.println("Sum = " + sum);
}catch(Exception e){System.out.println(e);}
}
//recursive function with exception is thrown with negative number
public static int addN(int num)throws Exception {
if(num<0)
throw new Exception("Number is negative,Enter a positive number!");
if (num != 0)
return num + addN(num - 1);
else
return num;
}
}

Output Screen - Test case 1

Output Screen - Test case 2

Know the answer?
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
Similar Questions
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to...
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to N. The function calculateSum has one parameter N of type integer and returns an integer which represents the sum from -1 to N, inclusive. Write another function calculateAverage that calculates an average. This function will have two parameters: the sum and the number of items. It returns the average (of type float). The main function should be responsible for all inputs and outputs. Your...
Write a python program to find the sum of the first n natural numbers, where the...
Write a python program to find the sum of the first n natural numbers, where the value of n is provided by the user. What is the sum of the first 200 numbers? Write a program that finds the average of a series of numbers entered by the user. As in the previous problem, the program will first ask the user how many numbers there are. Note: the average should always be a float. What is the average of 10.2,...
Write a Java program to randomly create an array of 50 double values. Prompt the user...
Write a Java program to randomly create an array of 50 double values. Prompt the user to enter an index and prints the corresponding array value. Include exception handling that prevents the program from terminating if an out of range index is entered by the user. (HINT: The exception thrown will be ArrayIndexOutOfBounds)
The Java program should sum the odd integers from 1 to the integer that the user...
The Java program should sum the odd integers from 1 to the integer that the user enters. For example, if the user enters 30, the program should sum the odd integers from 1 to 30.   Print "Please enter a positive nonzero integer." Declare a Scanner and obtain an integer from the user.   Use a Do...while statement and calculate the sum Use the integer that the obtained from the previous step for the loop-continuation condition. Display the sum Print the sum...
Step 1: Write a Java program that finds the sum of 10 numbers entered by the...
Step 1: Write a Java program that finds the sum of 10 numbers entered by the user. Step 2: Modify the previous program so that it asks the user how many numbers they have, inputs that many numbers, and finally outputs the average of their numbers. The messages to the user should be clear throughout.
Write a java program to find the average of several scores read by the user from...
Write a java program to find the average of several scores read by the user from keyboard, the program should stop accepting scores when the user enters a negative score, and then it displays the average for the scores
write a java program to find the sum of natural numbers from 1 to 1000 inclusive...
write a java program to find the sum of natural numbers from 1 to 1000 inclusive (using for loop) sample output: sum=500500
Write a complete recursive java program to compute the heights of the tick marks on a...
Write a complete recursive java program to compute the heights of the tick marks on a ruler. Assume that the length of the ruler is a power of 2 (say L=2^n, where n >=1) , and the marks are to be placed at every point between 0 and 2^n, not including the endpoints. The endpoints 0 and 2^n will have height 0. Here are the rules for computing the heights of the ticks for a ruler of length L=2^n: 1....
Write a program that adds the sum of digits in an integer and loops until 0...
Write a program that adds the sum of digits in an integer and loops until 0 is entered to end the program C++.
Write a program that asks the user of a positive integer value. The program should use...
Write a program that asks the user of a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1,2,3,4,…,50. using namespace std;
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT