196k views
0 votes
Write an if-else statement with multiple branches. If givenYear is 2100 or greater, print "Distant future" (without quotes). Else, if givenYear is 2000 or greater (2000-2099), print "21st century". Else, if givenYear is 1900 or greater (1900-1999), print "20th century". Else (1899 or earlier), print "Long ago". Do NOT end with newline. For Java

1 Answer

3 votes

Answer:

The program to this question in java can be given as

import java.util.Scanner;

//import package for take input from user.

public class Main //define class

{

public static void main(String[] args) // main function

{

Scanner input = new Scanner(System.in);

System.out.println("Enter a number:");

int givenYear=input.nextInt();

//if condition true output is "Distant future" (without quotes).

if (givenYear >= 2100)

{

System.out.print("Distant future");

}

// else if condition(2000-2099) true output is "21st century".

else if((givenYear >= 2000) && (givenYear <= 2099))

{

System.out.print( "21st century");

}

//else if condition (1900-1999) true output is "20th century".

else if((givenYear >= 1900) && (givenYear <= 1999)){

System.out.print( "20st century");

}

//else condition false output is "Long ago".

else{

System.out.print( "Long ago");

}

}

}

Step-by-step explanation:

In this program we use the scanner class. Scanner class is used to take input from the user.When the user input the input the year. It hold on a variable(givenYear) that is integer type.Then we check the value between the ranges that is given in question.To check all the condition we use the if-elseif-else statement, and AND(&&) operator.AND operator is a logical operator that is used for compare two values together. The output of the program can be given as:

Output:

Enter a number: 2016

21st century

User Vicky Salunkhe
by
7.4k points