Task - Control Flow Statements - Menu-driven Calculator #91
Replies: 15 comments
-
import java.util.*;
public class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number1:");
int num1 = sc.nextInt();
System.out.println("Enter number2:");
int num2 = sc.nextInt();
char c;
do {
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.println("5. Quit");
System.out.println("Enter your choice [1-5]:");
int userchoice = sc.nextInt();
switch (userchoice) {
case 1:
System.out.println("Sum of two numbers is :" + (num1 + num2));
break;
case 2:
System.out.println("Difference between two numbers is :" + (num1 - num2));
break;
case 3:
System.out.println("Multiplication of two numbers is : " + (num1 * num2));
break;
case 4:
System.out.println("Division of two numbers is :" + (num1 / num2));
break;
case 5:
break;
}
System.out.println("Do you want to continue? Y/N");
c = sc.next().charAt(0);
} while (c == 'Y');
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.Scanner;
public class menuDrivenCalc {
public static void performOperation(int choice, int a, int b) {
switch (choice) {
case 1:
add(a, b);
break;
case 2:
subtract(a, b);
break;
case 3:
multiply(a, b);
break;
case 4:
divide(a, b);
break;
default:
System.out.println("Ivalid Argument");
}
}
private static void divide(int a, int b) {
if (b == 0) {
System.out.println("You can't divide " + a + " by 0");
return;
}
System.out.println("Division of " + a + " and " + b + ": " + a / b);
}
private static void multiply(int a, int b) {
System.out.println("Multiplication of " + a + " and " + b + ": " + a * b);
}
private static void subtract(int a, int b) {
System.out.println("Subtraction of " + a + " and " + b + ": " + (a - b));
}
private static void add(int a, int b) {
int sum = a + b;
System.out.println("Addition of " + a + " and " + b + ": " + sum);
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int choice = 1;
System.out.println(" Simple Calculator");
while (choice != 5) {
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.println("5. Exit");
System.out.println("Enter Your Choice");
choice = s.nextInt();
System.out.println("Enter the two numbers:");
int a = s.nextInt();
int b = s.nextInt();
performOperation(choice, a, b);
}
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.*;
public class calc {
public static void main(String[] args) {
double a, b, result;
int n, condition;
Scanner scanner = new Scanner(System.in);
do {
System.out.println("Press 1 for addition");
System.out.println("Press 2 for subtraction");
System.out.println("Press 3 for Multiplication");
System.out.println("Press 4 for Division");
n = scanner.nextInt();
switch (n) {
case 1:
System.out.println("Enter the first number:");
a = scanner.nextDouble();
System.out.println("Enter the second number:");
b = scanner.nextDouble();
result = a + b;
System.out.println("The sum of the numbers " + a + " and " + b + " is = " + result);
break;
case 2:
System.out.println("Enter the first number:");
a = scanner.nextDouble();
System.out.println("Enter the second number:");
b = scanner.nextDouble();
result = a - b;
System.out.println("The difference of the numbers " + a + " and " + b + " is = " + result);
break;
case 3:
System.out.println("Enter the first number:");
a = scanner.nextDouble();
System.out.println("Enter the second number:");
b = scanner.nextDouble();
result = a * b;
System.out.println("The product of the numbers " + a + " and " + b + " is = " + result);
break;
case 4:
System.out.println("Enter the first number:");
a = scanner.nextDouble();
System.out.println("Enter the second number:");
b = scanner.nextDouble();
result = a / b;
System.out.println("In Division of " + a + " and " + b + "The Quotient is = " + result);
System.out.println("The remainder is = " + (a % b));
break;
default:
System.out.println("Invalid choice!! Please make a valid choice !!!");
}
System.out.println("To continue, Press 1 else Press any button to exit");
condition = scanner.nextInt();
} while (condition == 1);
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
String s;
Scanner sc = new Scanner(System.in);
do {
System.out.println("Enter a number: ");
int x1 = sc.nextInt();
System.out.println("Enter another number: ");
int x2 = sc.nextInt();
System.out.println("Calculator\n 1.Add \n 2.Substract \n 3.Multiplication \n 4.Division \n 5.Quit");
int n = sc.nextInt();
switch (n) {
case 1:
System.out.println("Sum = " + (x1 + x2));
break;
case 2:
System.out.println("Sub = " + (x1 - x2));
break;
case 3:
System.out.println("Multiplication = " + (x1 * x2));
break;
case 4:
System.out.println("Division = " + (x1 / x2));
break;
case 5:
System.exit(0);
}
System.out.println("If you want to continue enter Y else enter any other character");
s = sc.next();
} while (s.equals("Y"));
}
} |
Beta Was this translation helpful? Give feedback.
-
package Assignments;
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Welcome to Cal !");
int num1,num2,res,choice;
String continueChoice;
Scanner sc= new Scanner(System.in);
System.out.println("Enter Num 1: ");
num1=sc.nextInt();
System.out.println("Enter Num 2: ");
num2=sc.nextInt();
System.out.println("Enter your choice[1-5] :");
loop:do {
System.out.println(" 1. Addition\n 2. Subtraction\n 3. Multiplication\n 4.Division\n 5.Quit");
choice=sc.nextInt();
switch(choice) {
case 1:
res=num1+num2;
System.out.println("Addition of "+ num1+" and "+num2+" : "+res);
break;
case 2:
res=num1-num2;
System.out.println("Subtraction of "+ num1+" and "+num2+" : "+res);
break;
case 3:
res=num1*num2;
System.out.println("Multiplication of "+ num1+" and "+num2+" : "+res);
break;
case 4:
res=num1/num2;
System.out.println("Division of "+ num1+" and "+num2+" : "+res);
break;
case 5:
break loop;
default:
System.out.println("Invalid input. Please try again...");
break;
}
System.out.print("Do you want to continue? [Y/N]: ");
continueChoice = sc.next();
}while(continueChoice.equalsIgnoreCase("Y"));
System.out.println("Thank you for choosing Cal.");
sc.close();
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.*;
public class calc {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
System.out.println("Enter the 1st number");
int n1 = scn.nextInt();
System.out.println("Enter the 2nd number");
int n2 = scn.nextInt();
int choice;
char c;
loop: do {
System.out.println("Possible operations:\n1.add\n2.subtract\n3.multiply\n4.divide\n5.modulus\n6.quit\n");
System.out.println("Enter your choice");
choice = scn.nextInt();
float res = 0;
switch (choice) {
case 1:
res = n1 + n2;
System.out.println(n1 + " + " + n2 + " = " + res);
break;
case 2:
res = n1 - n2;
System.out.println(n1 + " - " + n2 + " = " + res);
break;
case 3:
res = n1 * n2;
System.out.println(n1 + " * " + n2 + " = " + res);
break;
case 4:
res = n1 / n2;
System.out.println(n1 + " / " + n2 + " = " + res);
break;
case 5:
res = n1 % n2;
System.out.println(n1 + " % " + n2 + " = " + res);
break;
case 6:
break loop;
default:
System.out.println("Enter a proper choice");
break;
}
System.out.println("Do you want to continue?y/n");
c = scn.next().charAt(0);
} while (c == 'y');
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.*;
public class MenuDrivenCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String continueChoice = "N";
System.out.println("Welcome!");
loop: do {
System.out.println("Please enter number 1 : ");
int num1 = sc.nextInt();
System.out.println("Please enter number 2 : ");
int num2 = sc.nextInt();
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.println("5. Quit");
System.out.print("Enter Choice[1-5]: ");
int userChoice = sc.nextInt();
switch (userChoice) {
case 1:
System.out.printf("Result : %d \n ", num1 + num2);
break;
case 2:
System.out.printf("Result : %d \n", num1 - num2);
break;
case 3:
System.out.printf("Result : %d \n", num1 * num2);
break;
case 4:
System.out.printf("Result : %d \n", num1 / num2);
break;
case 5:
break loop;
default:
System.out.println("Invalid input. Please try again...");
break;
}
System.out.print("Do you want to continue? [Y/N]: ");
continueChoice = sc.next();
} while (continueChoice.equalsIgnoreCase("Y"));
System.out.println("Thank you for visiting.");
sc.close();
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.* ;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Welcome");
String userChoice ;
do{
userChoice ="" ;
System.out.println("Enter First Number: ");
int n1 = Integer.parseInt(sc.nextLine()) ;
System.out.println("Enter Second Number: ");
int n2 = Integer.parseInt(sc.nextLine());
System.out.println("1. Addition");
System.out.println("2. Substraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.println("5. Quit");
System.out.println("Enter Choice [1-5] : ");
int result = 0 ;
int option = Integer.parseInt(sc.nextLine()) ;
switch(option){
case 1 :
result = n1+n2 ;
System.out.println("Result : " + result);
System.out.println("Do you want to continue?[Y/N] : ");
userChoice = sc.nextLine();
break;
case 2 :
result = n1 - n2 ;
System.out.println("Result : " + result);
System.out.println("Do you want to continue?[Y/N] : ");
userChoice = sc.nextLine();
break;
case 3 :
result = n1*n2;
System.out.println("Result : " + result);
System.out.println("Do you want to continue?[Y/N] : ");
userChoice = sc.nextLine();
break;
case 4 :
result =n1/n2 ;
System.out.println("Result : " + result);
System.out.println("Do you want to continue?[Y/N] : ");
userChoice = sc.nextLine();
break;
case 5 :
System.out.println("Thank you for visiting");
break ;
default :
System.out.println("Invalid Option, Please try again.");
userChoice = "Y" ;
break;
}
}while(userChoice.equalsIgnoreCase("Y"));
if(userChoice.equalsIgnoreCase("N")){
System.out.println("Thank you for using the calculator.");
}
sc.close();
}
} |
Beta Was this translation helpful? Give feedback.
-
|
import java.util.Scanner; public class MenuCal { } } |
Beta Was this translation helpful? Give feedback.
-
//CALCULATOR
class Collectionss {
static int add(int a,int b)
{
return a+b;
}
static int mul(int a,int b)
{
return a*b;
}
static int div(int a,int b)
{
return a/b;
}
static int sub(int a,int b)
{
return a-b;
}
public static void main(String[] args) {
System.out.println("Welcome to you");
Scanner sc =new Scanner(System.in);
String s="";
do {
System.out.println("1.Addition");
System.out.println("2.Subtraction");
System.out.println("3.Multiplication");
System.out.println("4.Division");
System.out.println("5.Quit");
System.out.println("Enter the 2 numbers");
int n1=Integer.parseInt(sc.nextLine());
int n2=Integer.parseInt(sc.nextLine());
System.out.println("Enter the choice");
int n=Integer.parseInt(sc.nextLine());
switch (n) {
case 1: {
int addition= add(n1,n2);
System.out.println(addition);
}
break;
case 2: {
int subtraction= sub(n1,n2);
System.out.println(subtraction);
}
break;
case 3: {
int multiplication= mul(n1,n2);
System.out.println(multiplication);
}
break;
case 4: {
int division= div(n1,n2);
System.out.println(division);
}
break;
case 5:
break;
}
System.out.println("Are you want to continue or not if yes press Y or no press N");
s=sc.nextLine();
} while(s.equalsIgnoreCase("Y"));
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
cal/* Addition import java.util.*;
public class menuDriven {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String choice;
loop:do{
choice = "";
System.out.println("enter the value of a");
int a = Integer.parseInt(sc.nextLine());
System.out.println("enter the value of b");
int b = Integer.parseInt(sc.nextLine());
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. multiplication");
System.out.println("4. Division");
System.out.println("5. quit");
System.out.print("Enter Choice from 1 to 5->:");
int option = Integer.parseInt(sc.nextLine());
switch (option){
case 1:
System.out.println("sum is :"+ (a+b));
System.out.println("Do you want to continue? [Y/N]");
choice = sc.nextLine();
break;
case 2:
System.out.println("Sub is :"+ (a - b ));
System.out.println("Do you want to continue? [Y/N]");
choice = sc.nextLine();
break;
case 3:
System.out.println("mul is" + (a * b) );
System.out.println("Do you want to continue? [Y/N]");
choice = sc.nextLine();
break;
case 4:
if(b == 0){
System.out.println("divide by zero");
}
System.out.println("Div is :" + ( a / b ));
System.out.println("Do you want to continue? [Y/N]");
choice = sc.nextLine();
break;
case 5:
break loop;
default :
System.out.println("Unknown Entry");
choice = "Y";
break;
}
} while (choice.equalsIgnoreCase("Y"));
if(choice.equalsIgnoreCase("N")){
System.out.println("You have pressed 'N', program exited !!");
}
sc.close();
}
} |
Beta Was this translation helpful? Give feedback.
-
Javaimport java.util.Scanner; } } |
Beta Was this translation helpful? Give feedback.
-
|
import java.util.*; |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Create a menu-driven calculator which performs the following arithmetic operations on two user-input numbers:
The program should ask the user whether they want to continue or not after every calculation.
Beta Was this translation helpful? Give feedback.
All reactions