Write a Java test program, all the code should be in a single main method, that
ID: 3916845 • Letter: W
Question
Write a Java test program, all the code should be in a single main method, that prompts the user for two integers and then prints the
sum
difference
product
quotient of the first integer divided by the second integer
remainder of the first integer divided by the second integer
maximum
minimum
Format your output using printf. Please label each of your answers.
Please submit your .java file for grading.
Starter Code
import java.util.Scanner;
public class MathFun
{
public static void main(String[] args)
{
// your code here
}
}
Explanation / Answer
MathFun.java
import java.util.Scanner;
public class MathFun
{
public static void main(String[] args)
{
// your code here
Scanner scan = new Scanner(System.in);
System.out.println("Enter the first number: ");
int first = scan.nextInt();
System.out.println("Enter the second number: ");
int second = scan.nextInt();
System.out.printf("Sum = %d ", first + second);
System.out.printf("Difference = %d ", first - second);
System.out.printf("Product = %d ", first * second);
System.out.printf("Quotient = %d ", first / second);
System.out.printf("Remainder = %d ", first % second);
int max, min;
if(first > second){
max = first;
min = second;
} else {
max = second;
min = first;
}
System.out.printf("Maximum = %d ", max);
System.out.printf("Minimum = %d ", min);
}
}
Output:
Enter the first number:
10
Enter the second number:
3
Sum = 13
Difference = 7
Product = 30
Quotient = 3
Remainder = 1
Maximum = 10
Minimum = 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.