Saturday, November 1, 2014

Java switch calculator pro



Coding of calculator programme using switch case in java

import java.io.*;
public class calculator
{
public static void main(String[] args) throws Exception
{
int x, y;
BufferedReader b = new BufferedReader(new InputStreamReader(System.in)) ;
System.out.println("Enter two numbers for operation:");
x = Integer.parseInt(b.readLine());
y = Integer.parseInt(b.readLine());
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.println("enter your choice:");
int a= Integer.parseInt(b.readLine());
switch (a)
{
case 1:
System.out.println("Enter the number one=" + (x+y));
break;
case 2:
System.out.println("Enter the number two=" + (x-y));
break;
case 3:
System.out.println("Enetr the number three="+ (x*y));
break;
case 4:
System.out.println("Enter the number four="+ (x/y));
break;
default:
System.out.println("Invalid Entry!");
     }
  }
}

Calculator using Switch


public class calculator
{
public static void main (String [] args)
{
    Scanner sc=new Scanner(System.in);
    int a,b;
    char p='+';
    char m='-';
    char multi='*';
    char div='/';
    String character;   
       System.out.print("enter ur firt value : ");
        a=sc.nextInt();
        System.out.print("enter ur second value : ");
        b=sc.nextInt();
        System.out.print("enter ur operator +,-,*,/ : ");
        character=sc.nextLine();
          switch(p){
            case '+':
                System.out.print(a+b);
                break;
      }
    }   
}

 

Simple calculator using switch case statement in java


import java.util.Scanner;
public class calculator
{
 public static void main(String[] args)
{
  Scanner s=new Scanner(System.in);
  System.out.println("You have following choices : ");
  System.out.println("1. Addition");
  System.out.println("2. Subtraction ");
  System.out.println("3. Multiplication ");
  System.out.println("4. Division");
  System.out.println("Enter your choice : ");
  int i=s.nextInt();
 
  System.out.println("Enter first number ");
  int a=s.nextInt();
 
  System.out.println("Enter second number ");
  int b=s.nextInt();
  double result=0;
  switch(i)
  {
  case 1:
   result=a+b;
   break;
  case 2:
   result=a-b;
   break;
  case 3:
   result=a*b;
   break;
  case 4:
   if(b==0)
   {
    System.out.println("Division not possible");
    break;
   }
   else
    result=a/b;
  default:
   System.out.println("You have entered a wrong choice");
   }
   System.out.println("Result = "+result);
 }
}

No comments:

Post a Comment