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

Conditional Solutionss

The document provides 5 solutions to conditional statement problems in Java. Solution 1 uses an if/else statement to check if an integer entered by the user is greater than or less than 0. Solution 2 uses an if statement to check if a temperature is above 100 to determine if the user has a fever. Solution 3 uses a switch statement to print the day of the week corresponding to a week number entered by the user. Solution 4 sets the values of two variables but does not show the full code. Solution 5 uses logical operators and if/else statements to determine if a year entered by the user is a leap year.

Uploaded by

Adnan Khan
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)
12 views3 pages

Conditional Solutionss

The document provides 5 solutions to conditional statement problems in Java. Solution 1 uses an if/else statement to check if an integer entered by the user is greater than or less than 0. Solution 2 uses an if statement to check if a temperature is above 100 to determine if the user has a fever. Solution 3 uses a switch statement to print the day of the week corresponding to a week number entered by the user. Solution 4 sets the values of two variables but does not show the full code. Solution 5 uses logical operators and if/else statements to determine if a year entered by the user is a leap year.

Uploaded by

Adnan Khan
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
You are on page 1/ 3

CONDITIONAL STATEMENTS

SOLUTIONS
Solution 1:
import java.util.*;

public class Solution {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();

if (x > 0) {
System.out.println("x is greater than 0");
} else {
System.out.println("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) {
System.out.println("You have a fever");
} else {
System.out.println("You don't have a fever");
}
}
}

Solution 3:
import java.util.*;

public class Solution {

public static void main(String args[]) {


Scanner sc = new Scanner(System.in);

/* Input week number from user */

System.out.println("Enter week number(1-7): ");


int week = sc.nextInt();

switch(week) {

case 1:
System.out.println("Monday");

break;

case 2:
System.out.println("Tuesday");

break;

case 3:
System.out.println("Wednesday");
break;

case 4:

System.out.println("Thursday");
break;

case 5:

break;
case 6:

System.out.println("Saturday");

break;
case 7:

System.out.println("Sunday");

break;
default:
System.out.println("Invalid input! Please enter week number between
1-7.");

}
}

Solution 4:
Value of x = false & y = 63..
Solution 5:
import java.util.*;

public class Solution {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Input the year: ");
int year = sc.nextInt();

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

if (x && (y || z)) {
System.out.println(year + " is a leap year");
} else {
System.out.println(year + " is not a leap year");
}
}
}

You might also like