Q:

Java program to build a calculator

belongs to collection: Java Basic Programs

0

Given numbers and we have to perform mathematical operations using java program.

Here, we have following operations to be performed through this program,

  1. Addition
  2. Subtraction
  3. Division
  4. Multiplication
  5. Exit
  6.  

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program for simple mathematical operations (calculator) in java

import java.util.Scanner;

public class Calculator 
{
	public static void main(String args[])
	{
		// declare here
		float a,b,res;
		char choice, ch;
		Scanner S=new Scanner(System.in);
		
		do
		{
			// prepare menu for the user to see multiple operations.
			System.out.println("\n\nMain Menu : \n1.Addition\n2.Subtraction\n3.Division\n4.Multiplication\n5.Exit\n");
			
			// enter the choice
			System.out.print("Enter your choice : ");
			
			// read the input choice value.
			choice=S.next().charAt(0);
			
			// this loop will calculate different the operations value at different dofferent values.
			switch(choice)
			{ 
			    case '1':System.out.print("Enter two numbers : ");
			    a=S.nextFloat();
			    b=S.nextFloat();	
			    res=a+b;
			    System.out.print("Result : " +res);
			    break;
			     
			     case '2':System.out.print("Enter two numbers : ");
				 a=S.nextFloat();
				 b=S.nextFloat();	
				 res=a-b;
				 System.out.print("Result : " +res);
				 break;
				    
			     case '3':System.out.print("Enter two numbers : ");
				 a=S.nextFloat();
				 b=S.nextFloat();	
				 res=a/b;
				 System.out.print("Result : " +res);
				 break;
				    
			     case '4':System.out.print("Enter two numbers : ");
				 a=S.nextFloat();
				 b=S.nextFloat();	
				 res=a*b;
				 System.out.print("Result : " +res);
				 break;
				   
			     case '5':
				 System.exit(0);
				 break;
				 default : System.out.print("Wrong Choice.....\n");
				 break;
			}
	    }
		// loop works till the number 5 not selected.
	  while(choice!=5);
	}
}

Output

Main Menu : 
1.Addition
2.Subtraction
3.Division
4.Multiplication
5.Exit

Enter your choice : 1
Enter two numbers : 2 5
Result : 7.0

Main Menu : 
1.Addition
2.Subtraction
3.Division
4.Multiplication
5.Exit

Enter your choice : 2
Enter two numbers : 8 6
Result : 2.0

Main Menu : 
1.Addition
2.Subtraction
3.Division
4.Multiplication
5.Exit

Enter your choice : 3
Enter two numbers : 10 2
Result : 5.0

Main Menu : 
1.Addition
2.Subtraction
3.Division
4.Multiplication
5.Exit

Enter your choice : 4
Enter two numbers : 5 5
Result : 25.0

Main Menu : 
1.Addition
2.Subtraction
3.Division
4.Multiplication
5.Exit

Enter your choice : 5

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to calculate compound interest... >>
<< Java program to find mean of a given number ...