Share on Google+Share on Google+

Sushmita
switch case
1 Answer(s)      5 years and 2 months ago
Posted in : Java Beginners

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

Ads

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.");

        }
    }
}









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
Switch case + condition??
Switch case + condition??  I try to use a switch case with condition...(); } } else { List down = (List)display.getCurrent(); switch(down.getSelectedIndex()) { case 0: Tambo(); if( label.equals
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
Switch Case - Java Interview Questions
Switch Case  int x=100000; switch(x) { case 100000: printf("Lacks"); } output= Lacks I want reason why x matches switch case although range for int is in between something -32678 to +32676. Plz give me ans quickly
PHP Switch Case
Switch Case Control Structure: Almost every modern language supports switch case control structure. It is similar to if statements. Switch case is useful... should know how the switch case works, in switch case every case is considered
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
Find Capital using Switch-Case statement
Find Capital using Switch-Case statement In this Java Tutorial section, we are going to find the capital of our states using Switch-Case statement. For this, we have created two arrays i.e. array of states and array of capitals. Then we
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   
Case Java Keyword
switch () { case 1: <statements> break; case 2... Case Java Keyword       The java keyword "case" is used to label each branch in the switch
switch statement
switch statement   i want to write a java program that computes Fibonacci,factorial,string reversal and ackerman using switch case to run as a single program
Switch
. Here is the general form of switch statement: switch (expression){ case 1.... Then there is a code block following the switch statement that comprises of multiple case... C:\javac>java Switch friday In this case, "
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
Java switch statement
Java switch statement  What restrictions are placed on the values of each case of a switch statement
programes on switch
=input.nextInt(); switch(choice){ case 1...="+add); break; case 2...); break; case 3: double mul
Switch databases
Switch databases  How do you inter-change between relational databases without code changes
Switch statement in PHP
Switch statement in PHP  HII, Explain about switch statement in PHP?   hello, Switch statement is executed line by line. PHP executes the statement only when the case statement matches the value of the switch
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 Statement with Strings in Java
Switch Statement with Strings in Java  Switch Statement with Strings in Java
switch functions - Java Beginners
your month of hire:") switch(n) { case(n="January"): document.write("Last... var n=0; n=prompt("Enter your month of hire:") switch(n) { case(n...switch functions  I am writing a script for use in a computer based
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
Business case
Business case  hii, For which phase of RUP the "business case" for the project established ?   hello, Inception phase of RUP the "business case" for the project established
using switch and break
using switch and break  generate a 10 digit number and display the length of longest increasing series
Tutorials   
Java Spring Hibernate Struts Training Retrieve database from the table dynamically in jsp from oracle using servlet What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized

Ads

 
Advertisement null

Ads