In: Computer Science
In java. Please explain. Consider the following program: } import java.util.*; public class Chapter7Ex12 { static Scanner console = new Scanner(System.in); public static void main(String[] args) { double num1; double num2; System.out.print("Enter two integers: "); num1 = console.nextInt(); num2 = console.nextInt(); System.out.println();
if (num1 != 0 && num2 != 0) System.out.printf("%.2f\n", Math.sqrt(Math.abs(num1 + num2 + 0.0))); else if (num1 != 0) System.out.printf("%.2f\n", Math.floor(num1 + 0.0)); else if (num2 != 0) System.out.printf("%.2f\n",Math.ceil(num2 + 0.0)); else System.out.println(0); }}
a. What is the output if the input is 12 4?
b. What is the output if the input is 3 27?
c. What is the output if the input is 25 0?
d. What is the output if the input is 0 49?
JAVA PROGRAM
import java.util.*;
import java.util.Scanner;
public class Chapter7Ex12
{
//scanner is class which helps in reeading a input from user
static Scanner console = new Scanner(System.in);
//main method
public static void main(String[] args) {
//declaring variables which is a double type
double num1;
double num2;
//prints the statement to enter two integers
System.out.println("Enter two integers:");
//when a user enter the first input it stores in num1
variable
num1 = console.nextInt();
//when a user enter the second input it stores in num2
varaible
num2 = console.nextInt();
System.out.println();
//checks the conditon entered first input and second
input which is not equal to 0 the if block is executed
if(num1!=0 && num2!=0)
//first it calculates the (num1+num2 )
//second Math.abs(num1+num2) abs is a function which
returns absolute value positive value ex: -177.89 it return
177.89
//third Math.sqrt(Math.abs(num1+num2+0.0)) sqrt is
function which returns the square of a number ex:16 square is
4
System.out.printf("%2f\n",Math.sqrt(Math.abs(num1+num2+0.0)));
//when a if block conditon fails else if block is executed
//checks the condition num1 varaible is equal to 0 or not
else if(num1!=0)
//prints the
//first calculate the num1+0.0
//math.floor(num1+0.0) Math.floor is function which
does not return decimal points example :100.078 it returns only
100.0
System.out.printf("%2f\n",Math.floor(num1+0.0));
//when a if and upper else if block fails this else if
is executed
//checks the condition entered second value is not
equal to 0
else if(num2!=0)
//prints the
//first calculate the num2+0.0
//math.ceil() is function which returns nearest value
of a decimal value ex:4.7 nearest is 5 it returns 5
System.out.printf("%2f\n",Math.ceil(num2+0.0));
//when none of upper block is executed then this else
block is executed
else
//prints the 0
System.out.println(0);
}
}
OUTPUTS
When Inout is 12 4
if block is executed because both input which is not equal to 0 which return the square root
12+4 = 16
16 square root is 4
when input 3 27
if block is executed because both input which is not equal to 0
3 + 27 = 30
30 square root is 5.47726
when input is 25 0
first
else if block is executed because first input which is not equal to 0
25+0 =25
when input is 0 49
2nd else if block is executed because first input is 0 if block fails and 1st else if block fails
second input 49 which is not equal to 0
0 +49 = 49
note : when if block is executed then no other else block is executed it checks the conditions if its meet then other block are not executed in case constion fails the other block will be executed
Thumbs Up