1) Create an interface to the keyboard (Scanner). Scanner is described in Chapter 2.
2) Prompt the user to enter their first and last name..
3) Read the name into a String.
4) Print a person greeting such as “Hello FirstName LastName”. Print the users name in the greeting.
5) Prompt the user to enter 2 integers.
6) Read the integers into 2 variables into your program.
7) Prompt the user to enter a math operation – one of +,-,*,/
8) Calculate a number in your program using the 2 variables and the operation. You will need either an if statement or switch statement to do this (recommend: if statement).
9) Print the result of the calculation as an equation (showing both numbers, the operation, an equal sign and the result of the operation).
10) Print a personalized goodbye message such as “Bye FirstName LastName”. Print the users name in the message.
Since you have not mentioned the language of your preference, I am providing the code in Java.
CODE
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String name;
System.out.println("Enter your first name and last name: ");
name = sc.nextLine();
System.out.println("Hello " + name);
int a, b;
System.out.println("Enter two integers: ");
a = sc.nextInt();
b = sc.nextInt();
System.out.println("Enter an operation among +,-,*,/ : ");
char op = sc.next().charAt(0);
int result = 0;
switch (op) {
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = a/b;
break;
default:
System.out.println("Invalid operation!!");
}
System.out.printf("%d %c %d = %d\n", a, op, b, result);
System.out.println("Bye " + name);
}
}
Get Answers For Free
Most questions answered within 1 hours.