Create a menu driven program using user defined functions to implement a calculator that performs the following:
a) Basic arithmetic operations(+,-,*,/)
b) log10(x),sin(x),cos(x)
Answers
Answer:
Q.1)
import java.util.Scanner;
public class calc
{
public static void main(String[] args)
{
int choice;
Scanner sc=new Scanner(System.in);
System.out.print("Enter 1 for addition .\nEnter 2 for subtraction.\n Enter 3 for multiplication.\nEnter 4 for division. ")
choice=sc.nextInt();
switch(choice)
{
case 1:
System.out.println("ADDITION");
System.out.print("ENTER A NUMBER: ")
double a=sc.nextDouble();
System.out.print("ENTER A NUMBER: ")
double b=sc.nextDouble();
double sum=a+b;
System.out.println("Sum is :"+sum);
break;
case 2:
System.out.println("SUBTRACTION");
System.out.print("ENTER A NUMBER: ")
double x=sc.nextDouble();
System.out.print("ENTER A NUMBER: ")
double y=sc.nextDouble();
double diff=x-y;
System.out.println("Difference is :"+diff);
break;
case 3:
System.out.println("MULTIPLICATION");
System.out.print("ENTER A NUMBER: ")
double p=sc.nextDouble();
System.out.print("ENTER A NUMBER: ")
double q=sc.nextDouble();
double mult=p*q;
System.out.println("Product is :"+mult);
break;
case 4:
System.out.println("DIVISION");
System.out.print("ENTER A NUMBER: ")
double c=sc.nextDouble();
System.out.print("ENTER A NUMBER: ")
double d=sc.nextDouble();
double quotient=c/d;
System.out.println("Quotient is :"+quotient); break;
}
}
}
Q.2)
import java.util.Scanner;
public class big_operations
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter 1 for getting log10(x).\nEnter 2 for getting sin(x).\n Enter 3 for getting cos(x).")
int choice=sc.nextInt();
switch (choice)
{
case 1:
System.out.println("log10(x)")
System.out.print("Enter the value of x: ")
double x=sc.nextDouble();
double log=Math.log10(x);
System.out.println("Log10(x) is "+log);
case 2:
System.out.println("sin(x)")
System.out.print("Enter the value of x: ")
double y=sc.nextDouble();
double sin=Math.sin(y);
System.out.println("sin(x) is "+sin);
case 3:
System.out.println("cos(x)")
System.out.print("Enter the value of x: ")
double z=sc.nextDouble();
double sin=Math.cos(z);
System.out.println("sin(x) is "+sin);
}
}
}