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
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started