1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
avanturin [10]
3 years ago
8

Write a program that prompts the user to enter the year and the first three letters of a month name (with the first letter in up

percase) and displays the number of days in the month.

Computers and Technology
1 answer:
elena55 [62]3 years ago
5 0

Answer:

I am writing a C++ program.

#include <iostream> // for input output functions

using namespace std; // to identify objects like cout, cin

int main() { // start of main() function body

   int year; // stores the year entered

   string month;  // holds the letters of a month

   cout<<"Enter the Year: "; // prompts user to enter year

   cin>>year; // reads year entered by user

   cout<<"Enter first three letters of a month ";

// prompts user to enter first three letters of a month

   cin>>month;    //reads the three letters of month entered by the user

// formula to determine leap year

   bool leapYear =((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);

//if checks if the 3 letters of input month match with one from these months  

if ( month == "Jan" || month =="Mar" || month == "May" || month == "Jul" || month == "Aug"|| month == "Oct"|| month == "Dec")

 //displays no of days in input month and year are 31

 cout<<"Number of days in "<<month<<" "<<year<<": 31 ";

//else- if checks if the input month matches with one from these months

else if (month =="Apr" || month=="Jun"|| month == "Sep"|| month == "Nov")

 //displays no of days in input month and year are 31

       cout<<"Number of days in "<<month<<" "<<year<<": 30 ";

else if (month =="Feb") //if the input month is Feb

cout<<"Number of days in "<<month<<" "<<year<<": "<<((leapYear) ? 29 : 28); // checks if the year is leap if yes then displays 29 days else 28

else //if none of the above if else conditions is true

{cout<<"Not a valid entry"; //displays invalid entry

    exit(0); //exits the program } }

             

Explanation:

The basic working of this program is briefly described in the comments above. I will explain the logic of the program.

This program takes int type year and a string month from the user (first three letters of the month).

Then it computes that if the year entered by the user is a leap year by this formula:

((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)

A leap year has 366 days and year which is not a leap year has 365 days. modulus operator is used here which returns the remainder of the division. If the input year is completely divisible by 4 which means we get remainder 0 then mod of year is taken with 100.  If the input year is evenly divisible by 100 then mode of year is taken with 400. Then if the input year is completely divisible by 400 this means the year is a leap year. Otherwise, year is not a leap year.

Next the IF statement is used to check the condition if the first three letters the month entered by the user matches one of the those mentioned in the if statement. As we know that the months Jan, March, May, July, Aug, Oct and Dec have 31 days so if the input month is any of these months then the message is displayed that the month has 31 days.

If the input month is not from the above months then else-if part is checked for input month. If the input month's letters matches with any of the months in else-if condition then the message displayed that the input year and month has 30 days.

If the above if and else if statement evaluate to false then the second else-if part is executed which checks if the input month is Feb, if its true the conditional statement checks if the input year is a leap year or not. If the input year is a leap year then the month Feb has 29 days otherwise 28 days.

If none of the above conditions are true this means that the user might be entering wrong letters such as Fab or Aig etc instead of Feb or Aug, so the message printed : Not a valid entry and program exits.

Screenshot of program along with its output is attached.

You might be interested in
The domain in an email message tells you the
Alexus [3.1K]
The type of service provider
8 0
3 years ago
What are the advantages of renting a home over buying a home? What are some disadvantages?
weqwewe [10]
An advantage of renting a home is that you don't have to fix any appliances or structures of the home your landlord does that for you. disadvantages are you have to follow their rules, you can't change anything on the inside like the paint or add a room to the home. in the state of Washington and landlord can evict you with only 20 days notice if they want to permanently sell the property. that's called a no-fault eviction. that's all I can think of hope that helps
7 0
3 years ago
What ip class is this address: 128-191.255.255.255?
defon
Class B would be the correct answer.
4 0
3 years ago
What does NBT stand for?​
Zanzabum

Answer:

National Benchmark Test

Explanation:

7 0
3 years ago
A system of classifying and organizing online content into categories by the use of user-generated metadata such as keywords is
SCORPION-xisa [38]

Answer:

Folksonomy.

Explanation:

A system of classifying and organizing online content into categories by the use of user-generated metadata such as keywords is called a folksonomy.

This ultimately implies that, folksonomy is a user-generated system which is typically used for classifying and organizing online content into various categories through the use of metadata such as keywords, electronic tags and public tags in order to make it easier to find in the future.

Hence, folksonomy is highly beneficial in areas such as collaborative learning, teacher resource repository, collaborative research, educational platforms, e-commerce etc.

6 0
2 years ago
Other questions:
  • What operating system allows various teams in its office to network and collaborate on projects
    5·1 answer
  • In the scene where we see Don and Lina acting in a silent film (beginning at time code 11:05), describe the silent film. How is
    7·1 answer
  • A video streaming website uses 32 bit integers to count the number of times each video is played. In anticipation of some videos
    14·2 answers
  • When naming a file you should use the _____________ instead of a space in a filename.
    6·1 answer
  • Consider the following recurrence, defined for n a power of 4 (for the time of some algorithm): T(n) = 3 if n = 1 2T(n/4) + 4n +
    10·1 answer
  • "In a web app, where is data usually stored? A. Mobile network B. Application storage C. Local computer D. Cloud storage"
    14·1 answer
  • A wireless network does not benefit like a wired network does, when it comes to collision reduction. Which device reduces collis
    6·1 answer
  • Wrtie a program in which we will pass a value N. N can be positive or negative. If N is positive then output all values from N d
    8·1 answer
  • What is computer generation.<br>name that generations ​
    9·2 answers
  • Why was it important for the date format to be standardized by the
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!