simple calculator for add, sub, multi, div, square and sqrt in java

import java.util.*; //importing utility class for user input
//class that contains all methods
class SimpleCalculator{
 int num1,num2,result,remainder;
 double result2;
 Scanner input=new Scanner(System.in); //for user input
 void add(){
  System.out.print("Enter number for addition : ");
  num1=input.nextInt();
  num2=input.nextInt();
 result = num1+num2;
 System.out.println("Sum of numbers is :" +result);
 }
 void sub(){
  System.out.print("Enter number for subtraction : ");
  num1=input.nextInt();
  num2=input.nextInt();
 result = num1-num2;
 System.out.println("Diff. of numbers is :" +result);
 }
 void div(){
  System.out.print("Enter number for division : ");
  num1=input.nextInt();
  num2=input.nextInt();
 result2 = num1/num2;
 remainder = num1%num2;
 System.out.println("Division of numbers is :" +result2  + remainder);
 }
 void multi(){
  System.out.print("Enter number for multiplication : ");
  num1=input.nextInt();
  num2=input.nextInt();
 result = num1*num2;
 System.out.println("multiplication of numbers is :" +result);
 }
 void sqr(){//square of any number
  System.out.print("Enter number for square : ");
  num1=input.nextInt();
 result = num1+num1;
 System.out.println("Sqr of numbers is :" +result);
 }
 void sqrt(){//for square root of number
  System.out.print("Enter number for squre root : ");
  num1=input.nextInt();
 result2 = Math.sqrt(num1);
 System.out.println("sqrt of numbers is :" +result2);
 }
}
/*
  Calculator is the main class, save program as Calculator.java than compile and run it
*/
class Calculator{
public static void main(String[] arr){
  SimpleCalculator obj = new SimpleCalculator();
 Scanner input = new Scanner(System.in);
System.out.println("Enter 1 for addition and 2 for subtracion");
System.out.println("Enter 3 for division and 4 for multiplication ");
System.out.println("Enter 5 for square and 6 for square root ");
int n = input.nextInt();
/*Switch is used for user choice what user want addition subtraction division etc. */
switch(n){
    case 1 :
obj.add();
break;
case 2 :
obj.sub();
break;
case 3 :
obj.div();
break;
case 4 :
obj.multi();
break;
case 5 :
obj.sqr();
break;
case 6 :
obj.sqrt();
break;
}
  }
}

/*
output :
Enter 1 for addition and 2 for subtracion
Enter 3 for division and 4 for multiplication
Enter 5 for square and 6 for square root
if user want to calculate multiplication enter 4
Enter number for multiplication :
4
8
multiplication of numbers is : 32
*/

Post a Comment

5 Comments

  1. For this web site, you will see our account, remember to go through this info. inches to feet

    ReplyDelete
  2. I finally found great post here.I will get back here. I just added your blog to my bookmark sites. thanks.Quality posts is the crucial to invite the visitors to visit the web page, that's what this web page is providing. calculator

    ReplyDelete
  3. I finally found great post here.I will get back here. I just added your blog to my bookmark sites. thanks.Quality posts is the crucial to invite the visitors to visit the web page, that's what this web page is providing. online antiderivative calculator

    ReplyDelete
  4. IIMT Group of College is a topmost B.Tech College in Delhi, Noida and NCR in India providing quality education since decades. The world class education makes it the best college in Delhi, Noida and NCR.
    top private engineering colleges in up

    ReplyDelete
  5. Anyone who has recently invested in a real estate property must be well aware of benefits of using an investment property calculator. This has now become an indispensable tool for real estate investors to calculate the viability of a particular deal and to estimate various expenses pertaining to the same. hours calculator week

    ReplyDelete