Challenge

Project

import java.util.Scanner;
import java.io.*;

public class Calculator {
	static Scanner sc = new Scanner(System.in);
	static int store;
	static File calculations = new File("CalculationsHistory.txt");

	public static void main(String[] args) {
		while (true) {
			System.out.println(" ");
			System.out.println("Scientific Calculator with the following operations");
			System.out.println("1. display operations 2. exit");
			int display = sc.nextInt();
			if (display == 1) {
				DisplayOperations();
			} else if (display == 2) {
				break;
			} else {
				System.out.println("invalid input");
			}
			System.out.println("Enter the number, which operation you want-");
			int choice = sc.nextInt();

			if (choice == 1) {
				Sum();
			} else if (choice == 2) {
				subtraction();
			} else if (choice == 3) {
				multiply();
			} else if (choice == 4) {
				divide();
			} else if (choice == 5) {
				powerOf();
			} else if (choice == 6) {
				squareRoot();
			} else if (choice == 7) {
				nthRoot();
			} else if (choice == 8) {
				logValue();
			} else if (choice == 9) {
				logn();
			} else if (choice == 10) {
				sine();
			} else if (choice == 11) {
				InverseSine();
			} else if (choice == 12) {
				Cosine();
			} else if (choice == 13) {
				InverseCos();
			} else if (choice == 14) {
				Tangent();
			} else if (choice == 15) {
				InverseTan();
			} else if (choice == 16) {
				stor();
			} else if (choice == 17) {
				recall();
			} else if (choice == 18) {
				d2b();
			} else if (choice == 19) {
				b2d();
			} else if (choice == 20) {
				sip();
			} else {
				System.out.println("invalid input try again.");
			}
		}
	}

	/*
	 * Use writeToFile method for binary to decimal operation same as we can use for
	 * all operations.
	 */
	private static void writeToFile(String a, String b) {

		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(calculations, true));
			bw.write("Enter the Binary number ");
			bw.newLine();
			bw.write(a);
			bw.newLine();
			bw.write("decimal number of " + a + " Binary is = ");
			bw.write(b);
			bw.newLine();
			System.out.println("writing to file is successfull");
			bw.close();
		} catch (IOException e) {
			System.out.println("writing to file us unsuccessfull");
			e.printStackTrace();
		}
	}

	private static void sip() {
		System.out.print("Enter the principle amount to invest: ");
		double p = sc.nextInt();
		System.out.print("Enter interest rate yearly: ");
		double r = sc.nextDouble();
		System.out.print("Enter the time period in years: ");
		double y = sc.nextInt();
		double monthlytime = y * 12;
		double monthlyrate = r / 12;
		double quarterlytime = y * 4;
		double quarterlyrate = r / 4;
		double yf = p * Math.pow((1 + r / 100), y);
		System.out.println("amount after yearly compounding: " + yf);
		double qf = p * Math.pow((1 + quarterlyrate / 100), quarterlytime);
		System.out.println("amount after quarterly compounding: " + qf);
		double mf = p * Math.pow((1 + monthlyrate / 100), monthlytime);
		System.out.println("amount after monthly compounding: " + mf);
	}

	private static void b2d() {
		System.out.println("Enter the Binary number ");
		sc.nextLine();
		String a = sc.nextLine();
		int b = Integer.parseInt(a, 2);
		String s = Integer.toString(b);
		System.out.println("decimal number of " + a + " Binary is = " + b);
		writeToFile(a, s); // writing input and result to the txt file.
	}

	private static void d2b() {
		System.out.println("Enter the Decimal number ");
		int a = sc.nextInt();
		System.out.println("Binary of " + a + " is = " + Integer.toBinaryString(a));
	}

	public static void recall() {
		int recall = store;
		System.out.println(recall);
	}

	public static void stor() {
		System.out.println("Enter the number to store in memory ");
		store = sc.nextInt();
	}

	private static void InverseTan() {
		System.out.println("Enter the number for Inverse tan of ");
		double x = sc.nextInt();
		System.out.println("itan of " + x + " is = " + Math.atan(x));
		double a = Math.atan(x);
		double b = Math.toDegrees(a);
		System.out.println("itan in degree " + b);
	}

	private static void Tangent() {
		System.out.println("Enter the number for Tangent of ");
		double x = sc.nextInt();
		double a = Math.toRadians(x);
		System.out.println("tan of " + x + " is = " + Math.tan(a));
	}

	private static void InverseCos() {
		System.out.println("Enter the number for Inverse cos of ");
		double x = sc.nextInt();
		System.out.println("icos of " + x + " is = " + Math.acos(x));
		double a = Math.acos(x);
		double b = Math.toDegrees(a);
		System.out.println("icos in degree " + b);
	}

	private static void Cosine() {
		System.out.println("Enter the number for Cosine of ");
		double x = sc.nextInt();
		double a = Math.toRadians(x);
		System.out.println("cos of " + x + " is = " + Math.cos(a));
	}

	private static void InverseSine() {
		System.out.println("Enter the number for Inverse sine of ");
		double x = sc.nextInt();
		System.out.println("isin of " + x + " is = " + Math.asin(x));
		double a = Math.asin(x);
		double b = Math.toDegrees(a);
		System.out.println("isin in degree " + b);
	}

	private static void sine() {
		System.out.println("Enter the number for sine of ");
		double x = sc.nextInt();
		double a = Math.toRadians(x);
		System.out.println("sin of " + x + " is = " + Math.sin(a));
	}

	private static void logn() {
		System.out.println("Enter the number for Natural log ");
		int a = sc.nextInt();
		System.out.println("Natural log of " + a + " is = " + Math.log(a));
	}

	private static void logValue() {
		System.out.println("Enter the number for Logarithmic value ");
		int a = sc.nextInt();
		System.out.println("Logarithmic value of " + a + " is = " + Math.log10(a));
	}

	private static void nthRoot() {
		System.out.println("Enter the number for nth square Root ");
		int value = sc.nextInt();
		System.out.println("Enter the value of n");
		int n = sc.nextInt();
		System.out.println(n + "th root of " + value + " is " + Math.pow(value, 1.0 / n));
	}

	private static void squareRoot() {
		System.out.println("Enter the number for square Root ");
		int a = sc.nextInt();
		System.out.println("square Root of " + a + " is = " + Math.sqrt(a));
	}

	private static void powerOf() {
		System.out.println("Enter the first no. ");
		int x = sc.nextInt();
		System.out.println("Enter the second no. ");
		int y = sc.nextInt();
		System.out.println("x raised to the y = " + Math.pow(x, y));
	}

	private static void divide() {
		System.out.println("Enter the first no. ");
		int x = sc.nextInt();
		System.out.println("Enter the second no. ");
		int y = sc.nextInt();
		System.out.println("division of x/y numbers = " + Math.floorDiv(x, y));
	}

	private static void multiply() {
		System.out.println("Enter the first no. ");
		int x = sc.nextInt();
		System.out.println("Enter the second no. ");
		int y = sc.nextInt();
		System.out.println("multiply of x*y numbers = " + Math.multiplyExact(x, y));
	}

	private static void subtraction() {
		System.out.println("Enter the first no. ");
		int x = sc.nextInt();
		System.out.println("Enter the second no. ");
		int y = sc.nextInt();
		System.out.println("subtraction of x-y numbers = " + Math.subtractExact(x, y));
	}

	public static void Sum() {
		System.out.println("Enter the first no. ");
		int x = sc.nextInt();
		System.out.println("Enter the second no. ");
		int y = sc.nextInt();
		System.out.println("Addition of x+y numbers = " + Math.addExact(x, y));
	}

	public static void DisplayOperations() {
		System.out.println("1.  \"+\"-plus or addition");
		System.out.println("2.  \"-\" -minus or subtraction");
		System.out.println("3.  \"*\" -multiply by");
		System.out.println("4.  \"/\" -divided by");
		System.out.println("5.  \"^\"-raised to the power of- x raised to the y");
		System.out.println("6.  \"Sqrt\"- square root");
		System.out.println("7.  \"Nrt\"- nth root");
		System.out.println("8.  \"log\"- Logarithmic value");
		System.out.println("9.  \"logn\"- Natural log");
		System.out.println("10. \"sin\"- Sine");
		System.out.println("11. \"isin\" -Inverse sine");
		System.out.println("12. \"cos\"- Cosine");
		System.out.println("13. \"icos\"- Inverse cosine");
		System.out.println("14. \"tan\"- Tangent");
		System.out.println("15. \"itan\"- Inverse tan");
		System.out.println("16. \"stor\" -place a number in memory for later use");
		System.out.println("17. \"recall\"-recover the number from memory for immediate use");
		System.out.println("18. \"d2b\" -Decimal to Binary number");
		System.out.println("19. \"b2d\" -Binary to Decimal number");
		System.out.println(
				"20. \"sip\"- Calculations of monthly, quarterly and yearly SIP calculations for compounding investments.");
	}

}
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.