Question

In: Computer Science

Write a program named CheckMonth2 that prompts a user to enter a birth month and day....

Write a program named CheckMonth2 that prompts a user to enter a birth month and day.

Display an error message that says Invalid date if the month is invalid (not 1 through 12) or the day is invalid for the month (for example, not between 1 and 31 for January or between 1 and 29 for February).

If the month and day are valid, display them with a message. For example, if the month entered is 2, and the day entered is 17, the output should be 2/17 is a valid birthday.

This is the code i start with. (Answer is C# Please.)


using System;
using static System.Console;
class CheckMonth2
{
static void Main()
{
// Write your main here
}
}

Solutions

Expert Solution

using System;

class CheckMonth2{

public static void Main (string[] args) {

Console.WriteLine ("Enter day and month : ");

int day = Convert.ToInt32(Console.ReadLine());

int month = Convert.ToInt32(Console.ReadLine());

int days=getDays(month);

if(days==-1){

Console.WriteLine("Invalid month. Must be 1 to 12");

}

else if(day!=days){

Console.WriteLine("Invalid days. must be "+days);

}

else{

Console.WriteLine(month+"/"+day+" is a valid birthday");

}

}

static int getDays(int mm) {

    int res = -1;

    switch (mm) {

    case 1:

      res = 31;

      break;

    case 2:

        res = 29;

      break;

    case 3:

      res = 31;

      break;

    case 4:

      res = 30;

      break;

    case 5:

      res = 31;

      break;

    case 6:

      res = 30;

      break;

    case 7:

      res = 31;

      break;

    case 8:

      res = 31;

      break;

    case 9:

      res = 30;

      break;

    case 10:

      res = 31;

      break;

    case 11:

      res = 30;

      break;

    case 12:

      res = 31;

      break;

    }

    return res;

  }

}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

Write a program that prompts the user to enter a person’s date of birth in numeric...
Write a program that prompts the user to enter a person’s date of birth in numeric form such as 8-27-1980. The program then outputs the date of birth in the form: August 27, 1980. Your program must contain at least two exception classes: invalidDay and invalidMonth. If the user enters an invalid value for day, then the program should throw and catch an invalidDay object. Follow similar conventions for the invalid values of month and year. (Note that your program...
Write a new program named Bar that prompts the user to enter a positive integer. The...
Write a new program named Bar that prompts the user to enter a positive integer. The program should then display a line consisting of the entered number of asterisks using a while loop. If the user enters a number that is not positive, the program should display an error message (see example below). Example 1: Enter a positive number: 6 ****** Example 2: Enter a positive number: 11 *********** Example 3: Enter a positive number: -4 -4 is not a...
Write a program which prompts the user for the year of their birth, and which then...
Write a program which prompts the user for the year of their birth, and which then calculates their age (ignoring the month/day). Split the program into `main`, a "prompt for year" function which tests to make sure the user's input is a valid year, and a "calculate age" function which performs the actual calculation. (You can assume the current year is 2017.) for my intro to c++ class
python.Write a python program that prompts the user to enter the year and first day of...
python.Write a python program that prompts the user to enter the year and first day of the year, and displays the first day of each month in the year. For example, if the user entered the year 2020 and 3 for Wednesday, January 1, 2020, your program should display the following output: January 1, 2020 is Wednesday February 1, 2020 is Saturday …… December 1, 2020 is Tuesday
Develop a Java program that prompts the user to enter her birth year (e.g. 1980). The...
Develop a Java program that prompts the user to enter her birth year (e.g. 1980). The program then uses the current year (i.e. 2020) and displays age of the user (i.e. 40). The program should handle any exception that can occur due to the value entered by the user.
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
Problem 4 : Write a program that prompts the user to enter in an integer and...
Problem 4 : Write a program that prompts the user to enter in an integer and then prints as shown in the example below Enter an integer 5 // User enters 5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 Bye
IN C++ Write a program that prompts the user to enter the number of students and...
IN C++ Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how much the highest score differs from the average. Use a while loop. Sample Output Please enter the number of students: 4 Enter the student name: Ben Simmons Enter the score: 70 Enter the student name:...
Write a program that prompts the user to enter a series of strings, but with each...
Write a program that prompts the user to enter a series of strings, but with each string containing a small integer. Use a while loop and stop the loop when the user enters a zero. When the loop has finished, the program should display: the number of user inputs (not counting the final zero input). the total of the integers in the strings entered. the average of the integers accurate to one decimal place. Any help is greatly appreciated, this...
Write a program that prompts user to enter integers one at a time and then calculates...
Write a program that prompts user to enter integers one at a time and then calculates and displays the average of numbers entered. Use a while loop and tell user that they can enter a non-zero number to continue or zero to terminate the loop. (Switch statement) Write a program that prompts user to enter two numbers x and y, and then prompts a short menu with following 4 arithmetic operations: Chose 1 for addition Chose 2 for subtraction Chose...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT