Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Q3 theory:

JAVA CODE:
import java.util.Scanner; // import Scanner class for reading input values
import java.lang.Math; // import Math class for calculate mathematical operations

// Create class MathOperations


public class MathOperations
{
// implement Menu() function and return integer value
public static int Menu()
{
Scanner scr=new Scanner(System.in); // create Scanner object "scr"
int choice; // declare integer "choice"
// create menu
System.out.println("\n1. Perform all the Arithmetic Operations");
System.out.println("2. Convert input No to Hexadecimal, Octal base");
System.out.println("3. Find the log,Sin,Cos,Tan,Log,anti-Log of the input No");
System.out.println("4. Find Cube and Factorial of the input no");
System.out.println("5. Exit");
System.out.print("\nEnter Your Choice:");
choice=scr.nextInt(); // read choice in console
return choice; // return choice value
}
// implement factorial() function and return integer value
// it is recursive function
public static int factorial(int x)
{
if(x==0) return 1; // check x=0 then return 1
else
return x*factorial(x-1); // else, calculate x value recursively
}

public static void main(String args[])


{
Scanner scr=new Scanner(System.in); // create Scanner object "scr"
int x,y,ch; // declare integer variables x,y and ch
// create infinite while loop until choice=5 then exit from program
while(true){
   ch=Menu(); // call Menu() function
   // create switch control structure
   switch(ch){
   // if ch=1 then,
   case 1: // read x and y values from console
       System.out.print("Enter x Value:"); x=scr.nextInt();
       System.out.print("Enter y Value:"); y=scr.nextInt();
       System.out.printf("\nAddition of %d and %d is %d",x,y,x+y); // calculate Addition
       System.out.printf("\nSubtraction of %d and %d is %d",x,y,x-y); // calculate Subtraction
       System.out.printf("\nMultiplication of %d and %d is %d",x,y,x*y); // calculate Multiplication
       System.out.printf("\nDivision of %d and %d is %d",x,y,x/y); // calculate Division
       System.out.printf("\nRemainder of %d and %d is %d",x,y,x%y); // calculate Remainder value
       break;
   // if ch=2 then,
   case 2:
       // read x value from console
       System.out.print("Enter x Value:"); x=scr.nextInt();
       System.out.println("Hexadecimal Number:"+Integer.toHexString(x)); // print hexadecimal number
       System.out.println("Octal Number: "+Integer.toOctalString(x)); // print octal number
       break;
   // if ch=3 then,
   case 3:
       // read x value from console
       System.out.print("Enter x Value:"); x=scr.nextInt();
       System.out.println("Log Value: "+Math.log(x)); // calculate log value using Math class
       System.out.println("Sin Value: "+Math.sin(x)); // calculate sin value using Math class
       System.out.println("Cos Value: "+Math.cos(x)); // calculate cos value using Math class
       System.out.println("Tan Value: "+Math.tan(x)); // calculate tan value
       System.out.println("Anti Log Value: "+Math.pow(10,x)); // calculate anti-log value using Math
class
       break;
   // if ch=4 then,
   case 4:
       // read x value from console
       System.out.print("Enter x Value:"); x=scr.nextInt();
       System.out.printf("\nCube of %d is %d",x,(x*x*x)); // calculate cube value
       System.out.printf("\nFactorial of %d is %d",x,factorial(x)); // calculate factorial (calling factorial()
function)
       break;
   case 5:System.out.println("Thank You!");
       System.exit(0); // exit from program
      

       }
   }
}  
}

OUTPUT:

C:\>javac MathOperations.java

C:\>java MathOperations

1. Perform all the Arithmetic Operations


2. Convert input No to Hexadecimal, Octal base
3. Find the log,Sin,Cos,Tan,Log,anti-Log of the input No
4. Find Cube and Factorial of the input no
5. Exit

Enter Your Choice:1


Enter x Value:6
Enter y Value:5

Addition of 6 and 5 is 11
Subtraction of 6 and 5 is 1
Multiplication of 6 and 5 is 30
Division of 6 and 5 is 1
Remainder of 6 and 5 is 1
1. Perform all the Arithmetic Operations
2. Convert input No to Hexadecimal, Octal base
3. Find the log,Sin,Cos,Tan,Log,anti-Log of the input No
4. Find Cube and Factorial of the input no
5. Exit

Enter Your Choice:2


Enter x Value:10
Hexadecimal Number:a
Octal Number: 12

1. Perform all the Arithmetic Operations


2. Convert input No to Hexadecimal, Octal base
3. Find the log,Sin,Cos,Tan,Log,anti-Log of the input No
4. Find Cube and Factorial of the input no
5. Exit

Enter Your Choice:3


Enter x Value:45
Log Value: 3.8066624897703196
Sin Value: 0.8509035245341184
Cos Value: 0.5253219888177297
Tan Value: 1.6197751905438615
Anti Log Value: 1.0E45

1. Perform all the Arithmetic Operations


2. Convert input No to Hexadecimal, Octal base
3. Find the log,Sin,Cos,Tan,Log,anti-Log of the input No
4. Find Cube and Factorial of the input no
5. Exit

Enter Your Choice:4


Enter x Value:3

Cube of 3 is 27
Factorial of 3 is 6
1. Perform all the Arithmetic Operations
2. Convert input No to Hexadecimal, Octal base
3. Find the log,Sin,Cos,Tan,Log,anti-Log of the input No
4. Find Cube and Factorial of the input no
5. Exit

Q7 part A
Q2 theory

You might also like