Date, Day and YearWrite a C program that prompts the user to enter the date as three integer values for the month, the day in the month, and the year. The program should then output the date in the form 31st December 2010 when the user enters 12 31 2010, say. The program has to work out when superscripts “th”, “nd”, “st”, and “rd” need to be appended to the day value. The programmer should not forget 1st, 2nd, 3rd, 4th; and then 11th, 12th, 13th, 14th; and 21st, 22nd, 23rd, and 24th.Testcases:Input:02 11 2021Output:11th February 2021
Question
Date, Day and YearWrite a C program that prompts the user to enter the date as three integer values for the month, the day in the month, and the year. The program should then output the date in the form 31st December 2010 when the user enters 12 31 2010, say. The program has to work out when superscripts “th”, “nd”, “st”, and “rd” need to be appended to the day value. The programmer should not forget 1st, 2nd, 3rd, 4th; and then 11th, 12th, 13th, 14th; and 21st, 22nd, 23rd, and 24th.Testcases:Input:02 11 2021Output:11th February 2021
Solution
Here is a simple C program that fulfills your requirements:
#include <stdio.h>
int main() {
int day, month, year;
char *months[] = {"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
char *suffixes[] = {"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th", "st"};
printf("Enter date (MM DD YYYY format): ");
scanf("%d %d %d", &month, &day, &year);
if(day < 1 || day > 31 || month < 1 || month > 12) {
printf("Invalid date!\n");
return 1;
}
printf("%d%s %s %d\n", day, suffixes[day], months[month], year);
return 0;
}
This program first defines the names of the months and the suffixes for the days. It then prompts the user to enter a date in MM DD YYYY format. If the entered date is invalid, it prints an error message and exits. Otherwise, it prints the date in the desired format.
Similar Questions
shu wants to create a program that asks users to input their birthdates, including the day, month, and year. The program should then display the entered birthdate in the format "DD/MM/YYYY."Write a program to assist Ishu in achieving this task.Input format :The input consists of three space-separated integer values, representing the user's birthdate(day, month, and year).Output format :The output displays the user's birthdate in the format "Your birthday is on " followed by the formatted date DD/MM/YYYY.
Write a JAVA program in which take date(DDMMYYYY) from user and displaynext day date(DDMMYYYY) as output.Example:Input: date=09, month=-06, year=1992Output: date=10, month=-06, year=1992Note:-1. Consider condition for leap year2. Consider number of days in month of February based on leap year ( if leapyear then February days =29, else days = 28 ) )3. Consider number of days either 30 or 31 based on month entered by user
Write a C++ program to overload the + operator to increment the date object.Sample input23 //year10//month8//daySample output24 11 9
Which function is used to create a date with year, month, and day? aDATE() bDATEVALUE() cCURDATE() dDATEDIF()
Single File Programming QuestionProblem StatementMerlin is working on a calendar application that requires a leap-year verification feature for accurate event scheduling. Users input a year, and her program determines whether it's a leap year or not, aiding in precise calendar calculations. Help her write a program to determine if the given year is a leap year or not by using only switch statements and operators.Note: This question was asked in TCS coding test.Input format :The input consists of an integer representing the year.Output format :If the given year is a leap year, the output prints "<<Input year>> is a leap year."Otherwise, the output prints "<<Input year>> is not a leap year."Refer to the sample output for the formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1900 ≤ Input year ≤ 2100
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.