Question: Use the Summation recursive program you did in the class to also work with minus integers. For example, the sum of -3 will be -6

Use the Summation recursive program you did in the class to also work with minus integers.

For example, the sum of -3 will be -6 which is (-3)+(-2)+(-1)+0. USE THIS CODE

package project5;

import java.util.Scanner;

public class SingleRecursion { /** Main method */ public static long sum(int n) { if (n<0) throw new IllegalArgumentException ("Can't calculate factorial of negative"); if (n==1) return 1; else if (n==0) return 1; else return n+sum(n-1); //return (n<2) ? 1 : n*factorial (n-1); } public static void main (String[] args) { Scanner stdIn = new Scanner (System.in); System.out.print("Factorial of what number do you want to calculate ? "); int num = stdIn.nextInt(); System.out.print( sum (num)); stdIn.close(); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!