Choose Your Language

Monday, 10 June 2013

Simple Calculator Java Program

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class SimpleCalculator {
public static void main(String a[]) {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    try {
        System.out.println("enter first value");
       
        int firstValue=Integer.parseInt(br.readLine());
        System.out.println("enter second value");
        int secondValue=Integer.parseInt(br.readLine());
        int result;
        System.out.println("select any one operation");
        System.out.println("+ - * /");
        char op=(char)br.read();
        if(op=='+'){
            result=firstValue+secondValue;
            System.out.println("selected operation is addition and the result is "+result);
        }
        else if(op=='-'){
            result=firstValue-secondValue;
            System.out.println("selected operation is subtraction and the result is "+result);
        }
        else if(op=='*'){
            result=firstValue*secondValue;
            System.out.println("selected operation is multiplication and the result is "+result);
        }
        else if(op=='/'){
            result=firstValue/secondValue;
            System.out.println("selected operation is division and the result is "+result);
        }
       
    } catch (NumberFormatException e) {
               e.printStackTrace();
    } catch (IOException e) {
               e.printStackTrace();
    }
}
}

Output:
enter first value
2
enter second value
6
select any one operation
+ - * /
-
selected operation is subtraction and the result is -4

No comments:

Post a Comment