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]
4 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]4 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
If Z=52 and ACT=48 then BAT=​ with explanation??​
Alekssandra [29.7K]
In the given code, A = 2, B = 4, C = 6,.... , Z = 52.
So, ACT = 2 + 6 + 40 = 48 and
BAT = 4 + 2 + 40 = 46
6 0
3 years ago
How to wire two separate switches &amp; lights using the same power source?
Ann [662]
Pull the other end of each cable through the back of the electrical box for the switch<span> that controls it, then strip the end of the cable with a knife and </span>separate<span> the </span><span>wires.

</span>
5 0
3 years ago
PLEASEEEEE HELLPPP IT'S URGENT!!!
Usimov [2.4K]

Answer:

Battery: DC power source to feed electricity in the circuit

Processor: component that reads instructions from memory, executes them and writes result data to memory and output devices

Accelerometer: device that measures motion acceleration along 3 dimenstions

Micro USB Port: standard interface for serial communication and power

Compass: sensor that finds orientation based on earth's magnetic field

•Block Editor: pass

Iteration: one cycle of a program loop

•Selection: pass

Variable: program construct to hold a value that code can read and write to.

Algorithm: programming code with a particular function to transform a given input to an output

Debug: the process of finding and fixing mistakes in your program

4 0
3 years ago
Grandma Ester normally gives you hugs for your birthday - one for every year old you are. When you turned 15, she squished you w
horsena [70]

Answer:

Written in Python

age = int(input("How old are you? "))

for i in range(1,age+1):

     print("**HUG**")

Explanation:

The first line prompts the user for age

age = int(input("How old are you? "))

The next line is an iteration that starts from 1 till the user input

for i in range(1,age+1):

The last line prints "**HUG**" while the iteration is true

     print("**HUG**")

4 0
3 years ago
Many Java programs that you create will receive and process user input. In order to ensure that user input is accurate, you will
Furkat [3]

Answer: True

Explanation: True

4 0
3 years ago
Other questions:
  • Jane is a postproduction crewmember for a film. Her responsibilities include arranging all the scenes that have been shot into t
    7·1 answer
  • Describe network in terms of the class computer lab
    14·1 answer
  • Public static Object[] question4(Student student1, Student student2)
    15·1 answer
  • What is an LMS and how is it used?
    8·1 answer
  • Defining a(n) ______________ of an object is called instantiation.
    12·1 answer
  • Question 7 of 10
    13·1 answer
  • Which is one use for a hyperlink? A. to create a heading style B. to animate important text C. to create a link to a website. D.
    7·1 answer
  • ) What does sampling mean?
    13·1 answer
  • _______ allows you to add formatting such as shapes and colors to text.
    14·1 answer
  • If someone wanted to talk to a financial institution representative in person they would need to _____.
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!