Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculator added via pkg2 by Mohak_Bajaj_500093079 #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions src/pkg2/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package pkg2;

import java.util.Scanner;

public class Calculator {
int add(int a, int b) {
return a + b;
}

int sub(int a, int b) {
return a - b;
}

int mul(int a, int b) {
return a * b;
}

int div(int a, int b) {
return a / b;
}

int mod(int a, int b) {
return a % b;
}

public void calculator() {
Scanner sc = new Scanner(System.in);
Calculator calc = new Calculator();
int a, b;
int choice = 0;
while (choice >= 0) {
System.out.println("Enter your choice: ");
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.println("5. Modulus");
System.out.println("0. Exit");
choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Enter two numbers: ");
a = sc.nextInt();
b = sc.nextInt();
System.out.println("Addition: " + calc.add(a, b));
break;
case 2:
System.out.println("Enter two numbers: ");
a = sc.nextInt();
b = sc.nextInt();
System.out.println("Subtraction: " + calc.sub(a, b));
break;
case 3:
System.out.println("Enter two numbers: ");
a = sc.nextInt();
b = sc.nextInt();
System.out.println("Multiplication: " + calc.mul(a, b));
break;
case 4:
System.out.println("Enter two numbers: ");
a = sc.nextInt();
b = sc.nextInt();
System.out.println("Division: " + calc.div(a, b));
break;
case 5:
System.out.println("Enter two numbers: ");
a = sc.nextInt();
b = sc.nextInt();
System.out.println("Modulus: " + calc.mod(a, b));
break;
case 0:
System.out.println("Exiting...");
choice = -1;
default:
System.out.println("Invalid choice!");
}
}
sc.close();
}
}
10 changes: 10 additions & 0 deletions src/pkg2/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package pkg2;

import pkg2.Calculator;

public class Main{
public static void main(String args[]){
Calculator c = new Calculator();
c.calculator();
}
}