136k views
3 votes
CHALLENGE

ACTIVITY
Multi-branch if-else statements: Print century.
Write an if-else statement with mulitiple branches.
If year is 2101 or later, print "Distant future" (without quotes). Otherwise, if year is 2001 or greater, print "21st century". Otherwise, if year is 1901 or greater, print "20th century". Else (1900 or earlier), print "Long ago".

User JPJedi
by
4.9k points

1 Answer

5 votes

Answer:

Following are the program to this question:

#include <stdio.h>//using the header file

int main()//main method

{

int y;//defining integer variable y

printf("Enter year value:");//print message

scanf("%d", &y);//use input method for input year value

if (y>= 2101)//defining if block that checks year greater then 2101

printf("Distant future");//print message

else if (y>= 2001)//defining else if block that checks year greater then 2001

printf("21st century"); //print message

else if (y>= 1901)//defining else if block that checks year greater then 1901

printf("20th century");//print message

else //defining else block

printf("Long ago");//print message

return 0;

}

Output:

Enter year value:1998

20th century

Step-by-step explanation:

In the given C language code, inside the main method, an integer variable "y" is defined, which allows the user to input the year value, and use the multiple conditions to check the given value and print message when the condition is matched.

  • Inside the "if" condition block, it checks the "y" variable value is greater and equal to 2101. so, it will print "Distant future", otherwise go to the next condition.
  • In this, if "y" value is greater than equal to "2001", it will print the message "21st century", otherwise go to the next condition.
  • In this, If the value of "y" is greater than equal to "1901", it will print the message "20th century", and if all the above condition is not true, then it will go to the else block, and it will print "Long ago" as the message.
User Souvik Basu
by
5.5k points