Question

Write a java calculator program that perform the following operations: 1. function to Addition of two...

Write a java calculator program that perform the following operations:
1. function to Addition of two numbers
2. function to Subtraction of two numbers
3. function to Multiplication of two numbers
4. function to Division of two numbers
5. function to Modulus (a % b)
6. function to Power (a b )
7. function to Square root of ()
8. function to Factorial of a number (n!)
9. function to Log(n)
10. function to Sin(x)
11. function to Absolute value (|x|)
12. function to Average of given numbers in array
Notes:
I. Use switch statement to select the required operation and invoke its appropriate method.
II. You should validate the input data before invoking any operation.
III. You should use different types of loops like (for, while, do…..while)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// OperationsOnNumbers.java

import java.util.Scanner;

public class OperationsOnNumbers {
   /*
   * Creating an Scanner class object which is used to get the inputs
   * entered by the user
   */
   static Scanner sc = new Scanner(System.in);
   public static void main(String[] args) {
       int a, b, choice;


       while (true) {
           displayMenu();
           // Getting the input entered by the user
           System.out.print("Enter Choice :");
           choice = sc.nextInt();
           switch (choice) {
           case 1: {
               a=getValidNumber();
               b=getValidNumber();
               System.out.println(a+" + "+b+" = "+(a+b));
               continue;
           }
           case 2: {
               a=getValidNumber();
               b=getValidNumber();
               System.out.println(a+" - "+b+" = "+(a-b));
               continue;
           }
           case 3: {
               a=getValidNumber();
               b=getValidNumber();
               System.out.println(a+" * "+b+" = "+(a*b));
               continue;
           }
           case 4: {
               a=getValidNumber();
               b=getValidNumber();
               System.out.println(a+" / "+b+" = "+(a/b));
               continue;
           }
           case 5: {
               a=getValidNumber();
               b=getValidNumber();
               System.out.println(a+" % "+b+" = "+(a%b));
               continue;
           }
           case 6: {
               a=getValidNumber();
               b=getValidNumber();
               System.out.println(a+" ^ "+b+" = "+(Math.pow(a,b)));
               continue;
           }
           case 7: {
               a=getValidNumber();
               System.out.println("Square Root of "+a+" = "+(Math.sqrt(a)));
               continue;
           }
           case 8: {
               a=getValidNumber();
               System.out.println("Factorial of "+a+" = "+factorial(a));
               continue;
           }
           case 9: {
               a=getValidNumber();
               System.out.println("Log("+a+") = "+Math.log(a));
               continue;
           }
           case 10: {
               a=getValidNumber();
               System.out.println("Sin("+a+") = "+Math.sin(a));
               continue;
           }
           case 11: {
               System.out.print("Enter a number :");
               a=sc.nextInt();
               System.out.println("Absolute of ("+a+") = "+Math.abs(a));          
               continue;
           }
           case 12: {
               int arr[]=new int[5];
               for(int i=0;i<arr.length;i++)
               {
                   System.out.print("Enter a number#"+(i+1)+":");
                   arr[i]=sc.nextInt();
               }
               System.out.println("Average of array numbers :"+calcAvg(arr));
               continue;
           }
           case 13: {
               break;
           }

           default: {
               System.out.println("** Invalid Choice **");
               continue;
           }

           }
           break;
       }

   }

   private static double calcAvg(int[] arr) {
       double sum=0;
       for(int i=0;i<arr.length;i++)
       {
           sum+=arr[i];
       }
       return (sum/arr.length);
   }

   private static long factorial(int a) {
long fact = 1;
do {
fact = fact * a;
a--;
} while (a > 0);
return fact;
   }

   private static int getValidNumber() {
       int num;
       while(true)
       {
           System.out.print("Enter a number :");
           num=sc.nextInt();
           if(num<0)
           {
               System.out.println("** Invalid.Must be positive number **");
           }
           else
               break;
       }
       return num;
   }

   private static void displayMenu() {

       System.out.println("\n1.function to Addition of two numbers");
       System.out.println("2. function to Subtraction of two numbers");
       System.out.println("3. function to Multiplication of two numbers");
       System.out.println("4. function to Division of two numbers");
       System.out.println("5. function to Modulus (a % b)");
       System.out.println("6. function to Power (a b )");
       System.out.println("7. function to Square root of ()");
       System.out.println("8. function to Factorial of a number (n!)");
       System.out.println("9. function to Log(n)");
       System.out.println("10. function to Sin(x)");
       System.out.println("11. function to Absolute value (|x|)");
       System.out
               .println("12. function to Average of given numbers in array)");
       System.out.println("13.Exit");
   }

}
____________________________

Output:


1.function to Addition of two numbers
2. function to Subtraction of two numbers
3. function to Multiplication of two numbers
4. function to Division of two numbers
5. function to Modulus (a % b)
6. function to Power (a b )
7. function to Square root of ()
8. function to Factorial of a number (n!)
9. function to Log(n)
10. function to Sin(x)
11. function to Absolute value (|x|)
12. function to Average of given numbers in array)
13.Exit
Enter Choice :1
Enter a number :3
Enter a number :4
3 + 4 = 7

1.function to Addition of two numbers
2. function to Subtraction of two numbers
3. function to Multiplication of two numbers
4. function to Division of two numbers
5. function to Modulus (a % b)
6. function to Power (a b )
7. function to Square root of ()
8. function to Factorial of a number (n!)
9. function to Log(n)
10. function to Sin(x)
11. function to Absolute value (|x|)
12. function to Average of given numbers in array)
13.Exit
Enter Choice :4
Enter a number :8
Enter a number :2
8 / 2 = 4

1.function to Addition of two numbers
2. function to Subtraction of two numbers
3. function to Multiplication of two numbers
4. function to Division of two numbers
5. function to Modulus (a % b)
6. function to Power (a b )
7. function to Square root of ()
8. function to Factorial of a number (n!)
9. function to Log(n)
10. function to Sin(x)
11. function to Absolute value (|x|)
12. function to Average of given numbers in array)
13.Exit
Enter Choice :5
Enter a number :9
Enter a number :2
9 % 2 = 1

1.function to Addition of two numbers
2. function to Subtraction of two numbers
3. function to Multiplication of two numbers
4. function to Division of two numbers
5. function to Modulus (a % b)
6. function to Power (a b )
7. function to Square root of ()
8. function to Factorial of a number (n!)
9. function to Log(n)
10. function to Sin(x)
11. function to Absolute value (|x|)
12. function to Average of given numbers in array)
13.Exit
Enter Choice :8
Enter a number :5
Factorial of 5 = 120

1.function to Addition of two numbers
2. function to Subtraction of two numbers
3. function to Multiplication of two numbers
4. function to Division of two numbers
5. function to Modulus (a % b)
6. function to Power (a b )
7. function to Square root of ()
8. function to Factorial of a number (n!)
9. function to Log(n)
10. function to Sin(x)
11. function to Absolute value (|x|)
12. function to Average of given numbers in array)
13.Exit
Enter Choice :12
Enter a number#1:5
Enter a number#2:6
Enter a number#3:7
Enter a number#4:8
Enter a number#5:9
Average of array numbers :7.0

1.function to Addition of two numbers
2. function to Subtraction of two numbers
3. function to Multiplication of two numbers
4. function to Division of two numbers
5. function to Modulus (a % b)
6. function to Power (a b )
7. function to Square root of ()
8. function to Factorial of a number (n!)
9. function to Log(n)
10. function to Sin(x)
11. function to Absolute value (|x|)
12. function to Average of given numbers in array)
13.Exit
Enter Choice :13

_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Write a java calculator program that perform the following operations: 1. function to Addition of two...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PLease IN C not in C++ or JAVA, Lab3. Write a computer program in C which...

    PLease IN C not in C++ or JAVA, Lab3. Write a computer program in C which will simulate a calculator. Your calculator needs to support the five basic operations (addition, subtraction, multiplication, division and modulus) plus primality testing (natural number is prime if it has no non-trivial divisors). : Rewrite your lab 3 calculator program using functions. Each mathematical operation in the menu should be represented by a separate function. In addition to all operations your calculator had to support...

  • Using c++.. 1. Write a program to find the sum(), Subtraction(), Multiplication(), Division() operations using Switch...

    Using c++.. 1. Write a program to find the sum(), Subtraction(), Multiplication(), Division() operations using Switch statement and functions. 2. Write a program to find the summation of N numbers. Use two functions. One function will take the input from user and the other will perform the summation from 1 to N. 3. Write a program to find the factorial of a number. Use two functions. One function will take the input from user and the other will perform the...

  • Write an ARM program that implements a simple four-function calculator and prompts the user to enter...

    Write an ARM program that implements a simple four-function calculator and prompts the user to enter a pair of decimal integers (A and B) followed by a character that specifies one of the operators: ‘+’ for addition to compute A+B                             ‘-‘ for subtraction to compute A-B ‘*’ for multiplication to produce the product A*B ‘/’ for division to produce the quotient A/B. Input should be the pair of numbers followed by the operand followed by a return. For example...

  • Section 3: Create a Calculator For this problem, please create a "calculator" function that takes two...

    Section 3: Create a Calculator For this problem, please create a "calculator" function that takes two integers and an operator and does the math operation corresponding to the numbers and the operator. For instance, calculate(5,"+", 6) should return 11. Please support the following operators: +- X/^(addition, subtraction, multiplication, division, exponentiation) 1 2 3 function calculate (numi, operator, num2) { // Do the calculation } RUN CODE Parameters Expected Result Actual Result 1,"+", 4 1,"x", 0 20,"7", 5 3,"", 3

  • Problem 3 (35) Design a calculator that performs four operations: addition, multiplication, division and subtraction with...

    Problem 3 (35) Design a calculator that performs four operations: addition, multiplication, division and subtraction with 2 integer type numbers a) Ask user what type of operation to perform (+, , * or/) a. If the user inputs 'none' then the program terminates. Otherwise it will keep continuing the calculation. (hint: use while) b) Ask user to provide 2 integer number inputs c) Perform operation (whatever operation they mentioned in a) d) Print result e) In division operation, perform a...

  • Creating a Calculator Program

    Design a calculator program that will add, subtract, multiply, or divide two numbers input by a user.Your program should contain the following:-The main menu of your program is to continue to prompt the user for an arithmetic choice until the user enters a sentinel value to quit the calculatorprogram.-When the user chooses an arithmetic operation (i.e. addition) the operation is to continue to be performed (i.e. prompting the user for each number, displayingthe result, prompting the user to add two...

  • Question 20: Write a python program that will perform the operations of simple calculator. The operations...

    Question 20: Write a python program that will perform the operations of simple calculator. The operations are : Addition, subtraction, Multiplication and division. Sample output : Select operation. 1.Add 2.Subtract 3.Multiply 4.Divide Enter choice(1/2/3/4): 3 Enter first number: 15 Enter second number: 14 15 * 14 = 210

  • Using Assembly Language (MIPS), design a program calculator that can perform the following: Addition, Subtraction, Multiplication,...

    Using Assembly Language (MIPS), design a program calculator that can perform the following: Addition, Subtraction, Multiplication, Division, Power and Square Root Functions. Code must be able to accept floating point as well.

  • Write an ARM program that implements a simple four-function calculator and prompts the user to enter...

    Write an ARM program that implements a simple four-function calculator and prompts the user to enter a pair of decimal integers (A and B) followed by a character that specifies one of the operators: ‘+’ for addition to compute A+B                             ‘-‘ for subtraction to compute A-B ‘*’ for multiplication to produce the product A*B ‘/’ for division to produce the quotient A/B. Input should be the pair of numbers followed by the operand followed by a return. For example...

  • Problem 9: (20 Points) Write a CH program to implement sum, subtraction, multiplication and division operations...

    Problem 9: (20 Points) Write a CH program to implement sum, subtraction, multiplication and division operations of two numbers. Please use functions and switch statements. Please provide option to the user to continue the program after each operation. Please provide option to 'Exit' the program also

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT