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

Solutions

This document contains 5 solutions to conditional statement problems in Java. Solution 1 checks if an integer input by the user is greater than 0 or less than/equal to 0. Solution 2 checks if a double variable representing body temperature is above or below 100 to determine if someone has a fever. Solution 3 uses a switch statement to print the day of the week corresponding to a numeric input between 1-7. Solution 4 assigns values to boolean variables to check conditions. Solution 5 checks if a year input by the user is a leap year or not.

Uploaded by

Ajay Kumar
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)
43 views3 pages

Solutions

This document contains 5 solutions to conditional statement problems in Java. Solution 1 checks if an integer input by the user is greater than 0 or less than/equal to 0. Solution 2 checks if a double variable representing body temperature is above or below 100 to determine if someone has a fever. Solution 3 uses a switch statement to print the day of the week corresponding to a numeric input between 1-7. Solution 4 assigns values to boolean variables to check conditions. Solution 5 checks if a year input by the user is a leap year or not.

Uploaded by

Ajay Kumar
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

APNA

COLLEGE

CONDITIONAL STATEMENTS SOLUTIONS

Solution 1:
import java.util.*;

t
public class Solution {

bo
public static void main (String l args) {

Scanner sc = new Scanner (System. in) ;

int x = sc.nextInt () ;

h_
if (x > 0) {

System. out.println ("x is greater than 0")


}

else {

System. out.println ("x is less


tc than or equal 0") ;
ba

Solution 2:
GE
a

public class Solution{

public static void main (String [] args) {


ph

double temp = 103.5;

if (temp > 100)

System. Out .println ("You have a fever") ;

)else
al

System. out.println ("You don't have a fever");


@

Solution 3:
import java.util.*;

public class Solution {


APNA
COLLEGE

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 ()

t
switch (week) {

case

bo
1:

System.out.println ("Monday") ;

break;

case 2:

System.out.println ("Tuesday") ;

h_
break;

case 3:

System.out .println ("Wednesday") ;

break;

case 4:
tc
System. out.println ("Thursday") ;

break;
ba
Case 5:

System.out.println ("Friday") ;

break;
case 6:
a

System. out.println ("Saturday" ) ;

break;
ph

case 7:
System. out.println ("Sunday ") ;

break
default:
al

System.out.println ("Invalid input! Please enter week number between


1-7.") 7
@

Solution 4:
Value ofx =
false & y 63..
APNA
COLLEGE

Solution 5:
1mport Java.util.*

public class Solution {

public static void main (String [] args) {

Scanner sc = new Scanner (System. in) ;

t
System.out.print ("Input the year: ") ;

bo
int year = sc.nextInt () ;

boolean x = (year 4) ==
0
boolean y = (year * 100) != 0;

z ((year * 100 0) (year 400= 0)) ;

h_
boolean = == &&

if (x & (y || z)) {

System. out.println (year +


tc"
is a leap year")
{
}else

System.out.println (year + "


is not a leap year") ;
ba

OLLEE
a
ph
al
@

You might also like