Concept explainers
a.
Explanation of Solution
Program code:
//Main class
class Main
{
//Main method
public static void main(String[] args)
{
//Declare the variable
int number = 14;
//int number = 15;
//int number = 30;
//Check if the number is even
if(number % 2 == 0)
//Print the statement
System.out.println(number + " is even");
//Check if the number is multiple of 5
if(number % 5 == 0)
//Print the statement
System.out.println(number + " is multiple of 5");
}
}
Output:
Here, whatever the number is given both the “if” condition is checked.
If the value “14” is assigned to the variable “number”:
“14 is even” gets printed because, “14” is an even number and the 1st “if” condition gets executed...
b.
Explanation of Solution
Program code:
//Main class
class Main
{
//Main method
public static void main(String[] args)
{
//Declare the variable
int number = 14;
//int number = 15;
//int number = 30;
//Check if the number is even
if(number % 2 == 0)
//Print the statement
System.out.println(number + " is even");
//Check if the number is multiple of 5
else if(number % 5 == 0)
//Print the statement
System.out.println(number + " is multiple of 5");
}
}
Output:
Here, if the “if” statement is true, then “else if” part does not gets executed...
Want to see the full answer?
Check out a sample textbook solutionChapter 3 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
- Find the output of code given below.arrow_forwardFor the given the following code segment, what is the output? And Why?arrow_forwardThis Code print the first occurrences of x, Please Modify the code to print the last occurrences of x and print how many times it exists? ( with explanation for each line if possible) many thanks.arrow_forward
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning