CSC211 LAB 6
- Write a program that uses while loops to perform the following steps.
- Prompt the user to input two integers: firstNum and secondNum (firstNum must be less than secondNum).
- Output all numbers between firstNum and secondNum
- Edit, compile and run the following program. This program provides a different solution using a switch statement.
import java.util.Scanner;
class CalculatorB {
public static void main(String [] args){
Scanner console = new Scanner(System.in);
int operand1,operand2;
char operator;
System.out.println(
"Enter two integers followed by one of the operators (+ - * /) ");
operand1 = console.nextInt();
operand2 = console.nextInt();
operator = console.next().charAt(0);
switch(operator){
case '+' :
System.out.println(operand1 + " + " + operand2 +
" = " + (operand1 + operand2));
break;
case '-' :
System.out.println(operand1 + " - " + operand2 +
" = " + (operand1 - operand2));
break;
case '*' :
System.out.println(operand1 + " * " + operand2 +
" = " + (operand1 * operand2));
break;
case '/' :
if(operand2 !=0 ){
System.out.println(operand1 + " / " + operand2 +
" = " + (operand1 / operand2));
}else{
System.out.println("Cannot divide by 0");
}
default:
System.out.println(operator + " Invalid Operator");
}
}
}