Sunday, 14 July 2013

Create a Calculator using Java program using Notepad

Write this program and save as "Calculator.java" then compile and run.

import java.util.Scanner;
class Calculator
{
public static void main(String args[])
{
Scanner in= new Scanner(System.in);
int ch;
float a,b,c;
do{
System.out.println("\n1 for Add, \n2 for Subs, \n3 for Multi, \n4 for Divide, \n5 for Exit.");
System.out.println("\nEnter Command:");
ch=in.nextInt();

switch(ch){
case 1:
System.out.println("Enter A:");
a=in.nextFloat();
System.out.println("Enter B:");
b=in.nextFloat();
c=a+b;
System.out.println(a+" + " + b + " = " + c);
break;
case 2:
System.out.println("Enter A:");
a=in.nextFloat();
System.out.println("Enter B:");
b=in.nextFloat();
c=a-b;
System.out.println(a+" - " + b + " = " + c);
break;
case 3:
System.out.println("Enter A:");
a=in.nextFloat();
System.out.println("Enter B:");
b=in.nextFloat();
c=a*b;
System.out.println(a+" * " + b + " = " + c);
break;
case 4:
System.out.println("Enter A:");
a=in.nextFloat();
System.out.println("Enter B:");
b=in.nextFloat();
c=a/b;
System.out.println(a+" / " + b + " = " + c);
break;
case 5:
break;
default:
System.out.println("Wrong Command!");
break;
}
}
while(ch!=5);
}
}

output:


Thanks for watching... comment it.

No comments:

Post a Comment