0% found this document useful (0 votes)
118 views3 pages

Java Conditional Statements Examples

This document contains 5 solutions to conditional statement problems in Java. Solution 1 uses an if/else statement to check if an integer input by the user is greater than 0. Solution 2 uses an if/else statement to check if a temperature variable is above 100 to determine if someone has a fever. Solution 3 uses a switch statement to output the day of the week corresponding to a numeric week input. Solution 4 sets values for variables x and y. Solution 5 uses logical operators and if/else statements to determine if a year input by the user is a leap year.

Uploaded by

HARSH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views3 pages

Java Conditional Statements Examples

This document contains 5 solutions to conditional statement problems in Java. Solution 1 uses an if/else statement to check if an integer input by the user is greater than 0. Solution 2 uses an if/else statement to check if a temperature variable is above 100 to determine if someone has a fever. Solution 3 uses a switch statement to output the day of the week corresponding to a numeric week input. Solution 4 sets values for variables x and y. Solution 5 uses logical operators and if/else statements to determine if a year input by the user is a leap year.

Uploaded by

HARSH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CONDITIONAL STATEMENTS SOLUTIONS

harshraj0235@[Link]
Solution 1:
import [Link].*;

public class Solution {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int x = [Link]();

if (x > 0) {
[Link]("x is greater than 0");
} else {
[Link]("x is less than or equal 0");
}
}
}

Solution 2:
public class Solution{
public static void main(String[] args) {
double temp = 103.5;
if (temp > 100) {
[Link]("You have a fever");
} else {
[Link]("You don't have a fever");
}
}
}

Solution 3:
import [Link].*;

public class Solution {


public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
/* Input week number from user */

harshraj0235@[Link]
[Link]("Enter week number(1-7): ");
int week = [Link]();

switch(week) {
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday");
break;
case 3:
[Link]("Wednesday");
break;
case 4:
[Link]("Thursday");
break;
case 5:
[Link]("Friday");
break;
case 6:
[Link]("Saturday");
break;
case 7:
[Link]("Sunday");
break;
default:
[Link]("Invalid input! Please enter week number between
1-7.");
}
}
}

Solution 4:
Value of x = false & y = 63..
Solution 5:
import [Link].*;

harshraj0235@[Link]
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Input the year: ");
int year = [Link]();

boolean x = (year % 4) == 0;
boolean y = (year % 100) != 0;
boolean z = ((year % 100 == 0) && (year % 400 == 0));

if (x && (y || z)) {
[Link](year + " is a leap year");
} else {
[Link](year + " is not a leap year");
}
}
}

You might also like