In: Computer Science
Write a java program that asks the user for a number n and gives them the possibility to choose between computing the sum and computing the product of 1,…,n.
Example of running this program:
Enter an integer number n: __7________
Enter Sum or Product: __Sum__________________________________
Program output: Sum of 1 ... 7
Sum or Product: Sum
Sum = 28
Now second sample of second execution
Enter an integer number n: __5__________________________________
Enter Sum or Product: __Product__________________________________
Program output: Product of 1 ... 5
Sum or Product: Product
Product = 120
import java.util.Scanner; public class SumProduct { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter an integer number n: "); int n = in.nextInt(); System.out.print("Enter Sum or Product: "); String choice = in.next(); System.out.println("Program output: " + choice + " of 1 ... " + n); int result; if (choice.equalsIgnoreCase("Sum")) { result = 0; for (int i = 1; i <= n; i++) result += i; } else { result = 1; for (int i = 1; i <= n; i++) result *= i; } System.out.println("Sum or Product: " + choice); System.out.println(choice + " = " + result); } }