Wednesday, 22 May 2013

SIMPLE CALCULATOR USING SWITCH CASE STATEMENT IN JAVA

This program is a simple calculator designed in java using switch case statement to perform some basic arithmetic operation on two integers entered by user.
It enables the user to enter a choice that corresponds to one of the four basic arithmetic operation, addition, subtraction, multiplication and division. The control structure used in this code is switch case, which works in the same manner as that in C or C++.


SOURCE CODE :-
/******************************************************************************/
import java.util.Scanner;

class Calculator 
{
	public static void main(String[] args) 
	{
		  
		Scanner s=new Scanner(System.in);
		System.out.println("YOU HAVE FOLLOWING CHOICES : ");
		System.out.println("1. ADDITION");
		System.out.println("2. SUBTRACTION ");
		System.out.println("3. MULTIPLICATION ");
		System.out.println("4. DIVISION");
		System.out.println("ENTER YOUR CHOICE : ");
		int i=s.nextInt();
		  
		System.out.println("ENTER FIRST NUMBER ");
		int a=s.nextInt();
		  
		System.out.println("ENTER SECOND NUMBER ");
		int b=s.nextInt();
		  
		double result=0;//'result' will store the result of operation
		  
		switch(i)
		{
			case 1:
				result=a+b;
				break;
			case 2:
				result=a-b;
				break;
			case 3:
				result=a*b;
				break;
			case 4:
				if(b==0)//when denominator becomes zero
				{
					System.out.println("DIVISION NOT POSSIBLE");
					break;
				}
				else
					result=a/b;
		   
			default:
				System.out.println("YOU HAVE ENTERED A WRONG CHOICE");
		   
		}
		  
		System.out.println("RESULT = "+result);
	}
}


/*****************************************************************************/

No comments:

Post a Comment