Google Search

Monday, March 2, 2015

write a Java program to perform arithmetic operations using switch statement

ALGORITHM:

Step 1: Start

Step 2: Read the numbers. Step 3: Print the choice menu
Step 4: Read the choice from the user.

Step 5: Perform the arithmetic operations using switch case. Step 6: Print the desired output
Step 7: Stop

PROGRAM: import java.io.*; class arith

{public static void main(String args[])throws IOException

{BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); float a,b,c;
System.out.println("Enter the values"); a=Float.parseFloat(br.readLine()); b=Float.parseFloat(br.readLine()); System.out.println("1.Add"); System.out.println("2.Sub"); System.out.println("3.Mul"); System.out.println("4.Div"); System.out.println("Enter your choice");int ch; ch = Integer.parseInt(br.readLine());c = 0; switch(ch)
{ case 1 : c=a+b; break ;
case 2 : c=a-b; break ;
case 3 : c=a*b; break ;
case 4 : c=a/b; break ;
default : System.out.println("Invalid choice"); break ;}
System.out.println("Result is " +c);

}

}




11


OUTPUT:

D:\JAVA>javac arith.java D:\JAVA>java arith Enter the values

10

10

1.Add

2.Sub

3.Mul

4.Div

Enter your choice 3

Result is 100.0 D:\JAVA>

0 comments:

Post a Comment