0% found this document useful (0 votes)
17 views1 page

Screen Shot 2

Uploaded by

R7D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views1 page

Screen Shot 2

Uploaded by

R7D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

C:/Users/omar1/OneDrive/NetBeans7/NetBeansProjects/Project/src/project/Project.

java
package project;
import java.util.Scanner;

public class Project {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
String choice;

System.out.println(" Calculator ");


System.out.println("=============================");
System.out.println("| x a^b a%b !a |");
System.out.println("| 7 8 9 * |");
System.out.println("| 4 5 6 ÷ |");
System.out.println("| 1 2 3 + |");
System.out.println("| 0 |x| log(x) - |");
System.out.println("| avg(a,b,...) sin(x) = |");
System.out.println("=============================");
System.out.println();

do {
System.out.println();
System.out.printf("| %-2s | %-36s |\n", "Numper", "Operation");
System.out.println("==============================================");
System.out.printf("| %-2s | %-40s |\n", "1", "Addition of two numbers");
System.out.printf("| %-2s | %-40s |\n", "2", "Subtraction of two numbers");
System.out.printf("| %-2s | %-40s |\n", "3", "Multiplication of two numbers");
System.out.printf("| %-2s | %-40s |\n", "4", "Division of two numbers");
System.out.printf("| %-2s | %-40s |\n", "5", "Modulus (a % b)");
System.out.printf("| %-2s | %-40s |\n", "6", "Power (a^b)");
System.out.printf("| %-2s | %-40s |\n", "7", "Square root of x");
System.out.printf("| %-2s | %-40s |\n", "8", "Factorial of a number");
System.out.printf("| %-2s | %-40s |\n", "9", "Log(n)");
System.out.printf("| %-2s | %-40s |\n", "10", "Sin(x)");
System.out.printf("| %-2s | %-40s |\n", "11", "Absolute value (|x|)");
System.out.printf("| %-2s | %-40s |\n", "12", "Average of numbers in array");
System.out.printf("| %-2s | %-40s |\n", "0", "Close");
System.out.println("==============================================");

choice = input.next();

switch (choice) {
case "1":

addition(input);
break;
case "2":
subtraction(input);
break;
case "3":
multiplication(input);
break;
case "4":
division(input);
break;
case "5":
modulus(input);
break;
case "6":
power(input);
break;
case "7":
squareRoot(input);
break;
case "8":
factorial(input);
break;
case "9":
log(input);
break;
case "10":
sin(input);
break;
case "11":
absolute(input);
break;
case "12":
average(input);
break;
case "0":
System.out.println("!Turn Off!");
break;
default:
System.out.println("Invalid choice.");
break;
}
} while (!choice.equals("0"));

public static void addition(Scanner input) {


try {
System.out.print("Enter the first numper : ");
double num1 = input.nextDouble();
System.out.print("Enter the second numper : ");
double num2 = input.nextDouble();
System.out.println("Result: " + (num1 + num2));
} catch (Exception e) {
System.out.println("Invalid input for addition.");

}
}

public static void subtraction(Scanner input) {


try {
System.out.print("Enter the first numper : ");
double num1 = input.nextDouble();
System.out.print("Enter the second numper : ");
double num2 = input.nextDouble();
System.out.println("Result: " + (num1 - num2));
} catch (Exception e) {
System.out.println("Invalid input for subtraction.");

}
}

public static void multiplication(Scanner input) {


try {
System.out.print("Enter the first numper : ");
double num1 = input.nextDouble();
System.out.print("Enter the second numper : ");
double num2 = input.nextDouble();
System.out.println("Result: " + (num1 * num2));
} catch (Exception e) {
System.out.println("Invalid input for multiplication.");

}
}

public static void division(Scanner input) {


try {

System.out.print("Enter the Numerator: ");


double Numerator = input.nextDouble();
System.out.println("Enter the Denominator");
double Denominator = input.nextDouble();
if (Denominator != 0) {
System.out.println("Result: " + (Numerator / Denominator));
} else {
System.out.println("Cannot divide by zero.");
}
} catch (Exception e) {
System.out.println("Invalid input for division.");

}
}

public static void modulus(Scanner input) {


try {
System.out.print("Enter the first numper : ");
int num1 = input.nextInt();
System.out.print("Enter the second numper : ");
int num2 = input.nextInt();
System.out.println("Result: " + (num1 % num2));
} catch (Exception e) {
System.out.println("Invalid input for modulus operation.");

}
}

public static void power(Scanner input) {


try {
System.out.print("Enter the Base : ");
double Base = input.nextDouble();
System.out.print("Enter the Exponent : ");
double Exponent = input.nextDouble();
System.out.println("Result: " + Math.pow(Base, Exponent));
} catch (Exception e) {
System.out.println("Invalid input for power operation.");

}
}

public static void squareRoot(Scanner input) {


try {
System.out.print("Enter the number: ");

double num = input.nextDouble();


if (num >= 0) {
System.out.println("Result: " + Math.sqrt(num));
} else {
System.out.println("Cannot calculate square root of a negative number.");
}
} catch (Exception e) {
System.out.println("Invalid input for square root operation.");

}
}

public static void factorial(Scanner input) {


try {
System.out.print("Enter an integer: ");
int num = (int)input.nextDouble();

if (num >= 0) {
long result = 1;
for (int i = 2; i <= num; i++) {
result *= i;
}
System.out.println("Result: " + result);
} else {
System.out.println("Factorial is not defined for negative numbers.");
}
} catch (Exception e) {
System.out.println("Invalid input for factorial.");
}
}

public static void log(Scanner input) {


try {
System.out.print("Enter the number: ");
double num = input.nextDouble();
if (num > 0) {
System.out.println("Result: " + Math.log10(num));
} else {
System.out.println("Logarithm undefined for non-positive numbers.");
}
} catch (Exception e) {
System.out.println("Invalid input for logarithm operation.");

public static void sin(Scanner input) {


try {
System.out.print("Enter an angle in Degree: ");
double angle_In_Degree = input.nextDouble();
double angle_In_Radians=Math.toRadians(angle_In_Degree);
System.out.println("Result: " + Math.sin(angle_In_Radians));
} catch (Exception e) {
System.out.println("Invalid input for sine operation.");

}
}

public static void absolute(Scanner input) {


try {
System.out.print("Enter the number: ");
double num = input.nextDouble();
System.out.println("Result: " + Math.abs(num));
} catch (Exception e) {
System.out.println("Invalid input for absolute value operation.");

}
}

public static void average(Scanner input) {


try {
System.out.print("Enter the number of elements: ");
int num = input.nextInt();
double[] array = new double[num];
double Avg = 0 ;
for (int i = 0; i < num; i++) {
System.out.println("Enter number " + (i + 1) + ": ");
array[i] = input.nextDouble();
Avg +=array[i];
}
System.out.println("The Average is : "+Avg/num );
} catch (Exception e) {
System.out.println("Invalid input for average calculation.");

}}

1.1 of 1 2024.11.26 07:29:08

You might also like