Question #82596

write a program that receives date and displays it displays it as following depending on user choice:date/month/year or dd/mm/yy

Expert's answer

#include <stdio.h>
int main(void)
{
// Defining variables
unsigned int year, month, day;
unsigned int dateChoice;
// Reading year
printf("Input year:");
scanf("%u", &year);
// Reading month
do{
printf("Input month(as number):");
scanf("%u", &month);
} while (month < 1 || month > 12);
// Reading day
do{
printf("Input day(as number):");
scanf("%u", &day);
} while (day < 1 || day > 31);
// Reading user choice for date format
do {
printf("Input display type. Enter 1 for day/month/year, enter 2 for dd/mm/yy:\n");
scanf("%u", &dateChoice);
} while (dateChoice != 1 && dateChoice != 2);
// Telling user what he've chosen
if (dateChoice == 1) printf("Your choice is day/month/year.\n");
else printf("Your choice is dd/mm/yy.\n");
// Displaying date
printf("The date is: ");
if (dateChoice == 1) { // day/month/year format
printf("%u/%u/%u\n", day, month, year);
}
else { // dd/mm/yy format
// year %100 is to get two last numbers of year for formatting
printf("%02u/%02u/%02u\n", day, month, (year % 100));
}
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS