Uploaded by vangonzales5728

fop

advertisement
Calculator & Prime Number Checker
import java.util.Scanner;
public class Main {
// It starts by importing the java util.Scanner; which
used for taking input by the user.
// This declares a public class named Main.
// The main method is the entry point of the
program.
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Initializes a boolean variable isRunning to true
to control the main program loop
boolean isRunning = true;
while (isRunning) {
//This is used to read input from user.
// The program enters a while loop that continues as
long as isRunning is true.
// Inside the loop, it displays the menu for the
user to choose an option (1. Basic calculator ,
2. Prime number checker, 3. Exit.)
System.out.println("\n
===========================================================");
System.out.println("
Welcome! Math Subject Helper at your service.");
System.out.println("
===========================================================\n");
System.out.println("\n
============================");
System.out.println("
| Choose an option:
|");
System.out.println("
| 1. Basic Calculator
|");
System.out.println("
| 2. Prime Number Checker |");
System.out.println("
| 3. Exit
System.out.println("
============================");
System.out.print("
|");
Enter: ");
int choice = scanner.nextInt();
// It reads the user’s choice as an integer
using the scanner.nextInt() method and stores
it in the choice variable.
// It uses a switch statement to perform different
switch (choice) {
actions based on the value of choice.
case 1:
basicCalculator(scanner);
break;
case 2:
primeNumberChecker(scanner);
break;
case 3:
isRunning = false;
// If the user chooses 1, it calls the basicCalculator
method, If the user chooses 2 it calls the
primeNumberChecker method. Lastly if the user
chooses 3, it sets isRunning to false, prints a
goodbye message, and exits the program.
System.out.println("\n
===========================================================");
System.out.println("
Exiting the program. Goodbye!
");
System.out.println("
===========================================================");
break;
default:
// If the user enters a value other than 1, 2, or 3, it prints an
error message.
System.out.println("\n\n
===========================================================");
System.out.println("
Invalid choice. Please enter a valid option.
");
System.out.println("
===========================================================");
break;
}
}
}
// basicCalculator (Scanner
public static void basicCalculator(Scanner scanner) {
scanner) method allows the user
to perform basic mathematical
operations on a set of numbers.
System.out.println("\n
System.out.print("
Basic Calculator");
Enter the number of values: ");
int count = scanner.nextInt();
double[] numbers = new double[count];
for (int i = 0; i < count; i++) {
System.out.print("
Enter number " + (i + 1) + ": ");
numbers[i] = scanner.nextDouble();
}
System.out.print("
Choose an operation (+, -, *, /): ");
char operation = scanner.next().charAt(0);
double result = numbers[0];
switch (operation) {
case '+':
for (int i = 1; i < count; i++) {
result += numbers[i];
}
break;
case '-':
for (int i = 1; i < count; i++) {
result -= numbers[i];
}
break;
case '*':
for (int i = 1; i < count; i++) {
result *= numbers[i];
}
break;
case '/':
for (int i = 1; i < count; i++) {
if (numbers[i] != 0) {
result /= numbers[i];
} else {
System.out.println("\n
System.out.println("\n
System.out.println("
============================");
Cannot divide by zero!");
============================\n\n\n\n\n");
return;
}
}
break;
default:
System.out.println("\n
============================");
System.out.println("\n
Invalid operation.");
System.out.println("
return;
============================\n\n\n\n\n");
// If the user enters an invalid operation, an error
message is printed, and the method returns.
}
System.out.println("\n
============================");
System.out.println("
System.out.println("
Result: " + result);
============== ==============\n\n\n\n\n");
// After the calculation, the result is printed along with
}
a separator.
public static void primeNumberChecker(Scanner scanner) {
//primeNumberChecker (Scanner scanner) method
it checks a given number is a prime number.
System.out.println("\n
System.out.print("
Prime Number Checker");
Enter a number: ");
int number = scanner.nextInt();
if (number <= 1) {
System.out.println("\n
============================");
System.out.println("
" + number + " is not a prime number.");
System.out.println("
============================\n\n\n");
} else {
boolean isPrime = true;
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.println("\n
============================");
System.out.println("
" + number + " is a prime number.");
System.out.println("
============================\n\n\n");
} else {
System.out.println("\n
System.out.println("
System.out.println("
}
}
}
============================");
" + number + " is not a prime number.");
============================\n\n\n");
}
Conclusion
The program continues to run , providing the menu options and performing the selected actions
until the user chooses to exit by entering the option 3.
Overall, this program is a simple consoled- based application and it will help the SHS student for
calculations and check for prime numbers interactively.
Members:
Gonzales, Leif Ivan
Laguras , Arniel Johan
Roxas, Khent Zyrelle
Download