Skip to content

Instantly share code, notes, and snippets.

@0x73hahd
Created December 14, 2022 03:48
Show Gist options
  • Save 0x73hahd/116e4abd5c33500d69ec384880146398 to your computer and use it in GitHub Desktop.
Save 0x73hahd/116e4abd5c33500d69ec384880146398 to your computer and use it in GitHub Desktop.
A simple calculator for a first-grade college project
import java.util.Arrays;
import java.util.Scanner;
public class Calculator {
static Scanner sc = new Scanner(System.in);
static double[] after_power = {};
static char answer = 'n';
public static void main(String[] args) {
switch (choose_operation()) {
case '+':
System.out.println(addition(array_of_numbers()));
break;
case '-':
System.out.println(subtraction(array_of_numbers()));
break;
case '/':
System.out.println(division(array_of_numbers()));
break;
case '%':
System.out.println(modulus(array_of_numbers()));
break;
case '*':
System.out.println(multiplication(array_of_numbers()));
break;
case '!':
System.out.println("Enter the num");
int factorial_num = sc.nextInt();
System.out.println(factorial(factorial_num));
break;
case '^':
System.out.println("Enter the power");
double power = sc.nextDouble();
after_power = power(array_of_numbers(), power);
System.out.println(Arrays.toString(after_power));
System.out.println("Do you want do more operation? (y/n)");
answer = sc.next().charAt(0);
if(answer == 'y' || answer == 'Y'){
perform_an_operation(choose_operation(), after_power);
}
break;
case 'Q':
break;
default:
System.out.println("No operation");
}
}
public static char choose_operation(){
System.out.println("Choose the operation ( +, -, /, %, *, ^, !)");
char operation = sc.next().charAt(0);
return operation;
}
public static double[] array_of_numbers(){
Scanner sc = new Scanner(System.in);
System.out.println("Please enter how many numbers you want ?:");
int number_of_inputs = sc.nextInt();
double[] numbers = new double[number_of_inputs];
System.out.println("Please enter your numbers");
for(int i = 0; i < number_of_inputs; i++){
numbers[i] = sc.nextDouble();
}
return numbers;
}
public static double addition(double numbers[]){
double result = 0;
for(int x = 0; x < numbers.length; x++){
result = result + numbers[x];
}
return result;
}
public static double subtraction(double numbers[]){
double result = 0;
for(int x = 0; x < numbers.length; x++){
result = result - numbers[x];
}
return result;
}
public static double division(double numbers[]){
double result = numbers[0];
for(int x = 1; x < numbers.length; x++){
if(numbers[x] == 0)
System.out.println("You can't divide by zero!");
result = result / numbers[x];
}
return result;
}
public static double modulus(double numbers[]){
double result = numbers[0];
for(int x = 1; x < numbers.length; x++){
if(numbers[x] == 0)
System.out.println("You can't divide by zero!");
result = result % numbers[x];
}
return result;
}
public static double multiplication(double numbers[]){
double result = 1;
for(int x = 0; x < numbers.length; x++){
result = result * numbers[x];
}
return result;
}
public static double[] power(double[] array, double power) {
double[] result = new double[array.length];
for (int i = 0; i < array.length; i++) {
result[i] = Math.pow(array[i], power);
}
return result;
}
public static int factorial(int number){
if(number == 0)
return 1;
return number * factorial(number - 1);
}
public static void perform_an_operation(char operation, double[] list_of_numbers){
switch (operation) {
case '+':
System.out.println(addition(list_of_numbers));
break;
case '-':
System.out.println(subtraction(list_of_numbers));
break;
case '/':
System.out.println(division(list_of_numbers));
break;
case '%':
System.out.println(modulus(list_of_numbers));
break;
case '*':
System.out.println(multiplication(list_of_numbers));
break;
case '^':
System.out.println("Enter the power");
double power = sc.nextDouble();
after_power = power(list_of_numbers, power);
System.out.println(Arrays.toString(after_power));
System.out.println("Do you want do more operation? (y/n)");
answer = sc.next().charAt(0);
break;
case 'Q':
break;
default:
System.out.println("Not an operation");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment