Problem:
Write an application that reads two floating point numbers and
prints their sum, difference, and product.
prints their sum, difference, and product.
Output:
Enter the first number: 54
Enter the second number: 12
Their sum is: 66.0
Their difference is: 42.0
Their product is: 648.0
Enter the second number: 12
Their sum is: 66.0
Their difference is: 42.0
Their product is: 648.0
Solution:
import java.util.Scanner;
class FloatCalculations
{
//-----------------------------------------------------------------
// Reads two floating point numbers and prints their sum,
// difference, and product.
//-----------------------------------------------------------------
public static void main (String[] args)
{
float num1, num2;
Scanner scan = new Scanner (System.in);
System.out.print ("Enter the first number: ");
num1 = scan.nextFloat();
System.out.print ("Enter the second number: ");
num2 = scan.nextFloat();
System.out.println ("Their sum is: " + (num1+num2));
System.out.println ("Their difference is: " + (num1-num2));
System.out.println ("Their product is: " + (num1*num2));
}
}
ReplyDeleteGiven an array of ints, return a new array length 2 containing the first and last elements from the original array. The original array will be length 1 or more.
ReplyDeleteGiven an array of ints, return a new array length 2 containing the first and last elements from the original array. The original array will be length 1 or more.