Menu driven program in java using switch case
Solution:
import java.io.*;
public class MenuSeries
{
public static void main(String args[])throws IOException
{
InputStreamReader in=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(in);
char choice;
int n,i;
double s=0;
System.out.print("Enter n number:");
n=Integer.parseInt(br.readLine());
System.out.println("A or a for series 0,3,8,15,24……");
System.out.println("B or b fro series s=1/2+3/4+5/6+..+19/20");
System.out.println("Enter your choice:");
choice=(char)br.read();
switch(choice)
{
case 'A':
case 'a':
for(i=1;i<=n;i++)
{
System.out.print((i*i)-1+",");
}
break;
case 'B':
case 'b':
for(i=2;i<=20;i+=2)
{
s=s+(double)(i-1)/i;
}
System.out.println("Sum="+s);
break;
default:
System.out.print("Wrong choice.....");
}
}
}
No comments:
Post a Comment