switch case

Ads
 

switch case

program to input 2 numbers and a choice.Using switch case we need to perform all the mathematical operation.Print a suitable error message if the choice is wrong

View Answers

February 17, 2012 at 4:08 PM

import java.util.*;
class SwitchCase 
{
    public static void main(String[] args) 
    {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter Number1: ");
        int num1=input.nextInt();
        System.out.print("Enter Number2: ");
        int num2=input.nextInt();
        System.out.println("1 Addition");
        System.out.println("2 Subtraction");
        System.out.println("3 Multiplication");
        System.out.println("4 Division");
        System.out.println("5 Exit");

        System.out.print("Enter your choice: ");
        int choice=input.nextInt();
        switch(choice){
         case 1:
             int sum=num1+num2;
             System.out.println("Result is "+sum);
             break;

         case 2:
             int diff=0;
             if(num1>num2){
                 diff=num1-num2;
             }
             else{
                 diff=num2-num1;
             }
             System.out.println("Result is "+diff); 
             break;

         case 3:
             int mul=num1*num2;
             System.out.println("Result is "+mul);
             break;

         case 4:
             int div=0;
             if(num1>num2){
                 div=num1/num2;
             }
             else{
                 div=num2/num1;
             }
             System.out.println("Result is "+div);
             break;

         case 5:
             System.exit(0);
             break;

             default: 
                 System.out.println("Choose correct mathematical operation.");

        }
    }
}

Ads









Related Tutorials/Questions & Answers:
switch case
switch case  program to input 2 numbers and a choice.Using switch case we need to perform all the mathematical operation.Print a suitable error message if the choice is wrong
switch case
switch case  program to input 2 numbers and a choice.Using switch case we need to perform all the mathematical operation.Print a suitable error message if the choice is wrong
Advertisements
switch case instead of if else
switch case instead of if else  How to write code in switch case instead of if else in Java
using switch case
using switch case  Define a class MENU to perform the following operation depending upon the users choice using switch case 1)print square root... choice: "); int choice=input.nextInt(); switch(choice
Switch case in Jsp page
Switch case in Jsp page  <table> <tr><td>Enter Distance :</td> <td><input type="text" name="dis"/></td>... using switch case in jsp page
ModuleNotFoundError: No module named 'switch-case'
ModuleNotFoundError: No module named 'switch-case'  Hi, My Python... 'switch-case' How to remove the ModuleNotFoundError: No module named 'switch-case' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'switch-case'
ModuleNotFoundError: No module named 'switch-case'  Hi, My Python... 'switch-case' How to remove the ModuleNotFoundError: No module named 'switch-case' error? Thanks   Hi, In your python environment
Switch case in java
Switch case in java In this section we will learn about switch case in java..., and java, switch case is used . switch  is a selection control mechanism used... will illustrate the use of switch case in java: import java.util.Scanner; public
Mysql Switch CASE
Mysql Switch CASE       Mysql Switch Case perform one of several different actions based... illustrate an example from 'Mysql Switch Case'.To understand an example from
Switch Case in Java
version of Java did not support String in switch/case. Switch statements works... A statement in the switch block can be labeled with one or more case or default labels... case will take place. Example of Switch Statement in Java: import java.io.
Write a java program to display the season given the month using switch case
Write a java program to display the season given the month using switch case  Write a java program to display the season given the month using switch case
i need to replace this if statement code with switch case code
i need to replace this if statement code with switch case code   i need to replace this code with switch case for(int i=0;i<100;i++){ if((i...){ switch(i/j){ case 3: System.out.println ("java
C Break with Switch statement
with Switch-Case in C. The switch case statements allows to control complex... the second case i.e odd If you enter the number 6, the switch statement checks... C Break with Switch statement   
Switch Statement
Switch Statement  How we can use switch case in java program ?   Note:-Switch case provides built-in multiway decision statement.It...); switch(days){ case 1: System.out.println("Sunday
The Switch statement
. Here is the general form of switch statement: switch (expression){ case 1.... The appropriate case gets executed when the switch statement evaluates its...Switch       Sometimes
programes on switch
=input.nextInt(); switch(choice){ case 1...="+add); break; case 2...); break; case 3: double mul
switch Java Keyword
, byte, short or int. then only the execution of the switch case will start. -- Within a switch case, a case block cannot be terminated itself i.e. it does not have... of each case block to exit from the switch statement. -- If the break statement
Switch databases
Switch databases  How do you inter-change between relational databases without code changes
change color of back ground using switch
change color of back ground using switch   how to change back ground color according to day using switch case
Java Switch Statement
then switch statement can be used. It checks your choice and jump on that case label...; switch Switch case is followed by a parenthesized integer expression... case value exist. Execution continues after the end of the switch statement
Switch Statement with Strings in Java
Switch Statement with Strings in Java  Switch Statement with Strings in Java
test case
test case  Hi Can you explain me how to write test case in java. regards kennedy
Matching Case
Matching Case  Hi, i want some code for matching case from an text file. i.e if i give an query called Java from the user,i need to get an output searching the text file all the names in that without case sensitive
Strings in Switch statement
have matched string. The comparison of string in switch statement is case...) { String typeOfDay; switch (dayOfWeekArg) { case "Monday": typeOfDay...Strings in Switch statement In this section, you will learn about strings
ModuleNotFoundError: No module named 'switch'
ModuleNotFoundError: No module named 'switch'  Hi, My Python... 'switch' How to remove the ModuleNotFoundError: No module named 'switch'... to install padas library. You can install switch python with following command
ModuleNotFoundError: No module named 'switch'
ModuleNotFoundError: No module named 'switch'  Hi, My Python... 'switch' How to remove the ModuleNotFoundError: No module named 'switch'... to install padas library. You can install switch python with following command
uisearchbar case sensitive
uisearchbar case sensitive  uisearchbar case sensitive
case study
case study  Chocolate Manufacturer Case Study CDA Company is in the business of manufacturing chocolates. The company delivers various chocolate products like- chocolate bars ,chocolate powder ,chocolate gift box and chocolate
Conditional Examples(if - else- switch case) in JavaScript
Conditional Examples(if - else- switch case) in JavaScript...; This example is display to all months in a year for using the switch case...;)  switch(n)  { case(n="1"): 
using switch,break and for loop
using switch,break and for loop  generate a 10 digit number and display the length of longest increasing series
using switch and break
using switch and break  generate a 10 digit number and display the length of longest increasing series

Ads