Choose Your Language

Thursday 4 April 2013

Simple Calculator 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.print("enter first value");       
        int firstValue=Integer.parseInt(br.readLine());

        System.out.print("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 addition operation and the result is "+result);
        }
        else if(op=='-'){
            result=firstValue-secondValue;
            System.out.println("selected subtraction operation and the result is "+result);
        }
        else if(op=='*'){
            result=firstValue*secondValue;
            System.out.println("selected multiplication operation and the result is "+result);
        }
        else if(op=='/'){
            result=firstValue/secondValue;
            System.out.println("selected division operation and the result is "+result);
        }
       
    } catch (NumberFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

output:
enter first value3
enter second value4
select any one operation
+ - * /
*
selected operation is multiplication and the result is 12

No comments:

Post a Comment