Tuesday, June 30, 2015

Make A simple Calculator In Java

import java.util.*;

public class CalculatorUsingMethods
 {
static double add(double num1, double num2)
{
return num1+num2;
}
static double subtract(double num1, double num2)
{
return num1-num2;
}
static double multiply(double num1, double num2)
{
return num1*num2;
}
static double division(double num1, double num2)
{
return num1/num2;
}
public static void main(String[] args)
{
Scanner scn=new Scanner(System.in);
System.out.println(" type first number ");
double num1=scn.nextDouble();
System.out.println(" type second no.");
double num2=scn.nextDouble();
System.out.println("type operator");
String op=scn.next();
if(op.equals("+"))
System.out.println(add(num1, num2));
else if(op.equals("-"))
System.out.println(subtract(num1, num2));
else if(op.equals("*"))
System.out.println(multiply(num1, num2));
else if(op.equals("/"))
System.out.println(division(num1, num2));
else
System.out.println(" bad operator");

}

}

0 comments:

Post a Comment