Write a menu driven program for the following options.
S=1+8+26+64+125+216.
(its 26 only, not 27)
Answers
Its actually 27 according to the logic nth term = n³. Here comes the solution to the problem.
import java.util.*;
public class MenuDriven{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("1. Find sum of series 1 + 8 + 27 + 125 + ... N terms.");
System.out.println("2. Exit.\n");
System.out.print("Enter your choice: ");
switch(sc.nextInt()){
case 1:
System.out.print("Enter limit: ");
int n,i,s=0;
n=sc.nextInt();
for(i=1;i<=n;i++)
s+=i*i*i;
System.out.print("Sum: "+s);
break;
case 2:
System.exit(0);
break;
default:
System.out.print("Invalid Choice.");
}
sc.close();
}
}
- Ask the user to enter his/her choice.
- If choice is 1, calculate the sum of the series.
- If choice is 2, exit the program.
- If choice is other than 1 or 2, display an invalid message.
See attachment for output.