
Write a C program that formats product information entered by the user.
The item code can be any number between 100 and 999, price is a dollar amount up to $9999.99
and date should be in mm/dd/yy format. Save the program in a file named as display.c,
and submit the file online as your solution to this question. A session with the program should
look like this:
// Product Information Display Program
#include <stdio.h>
int main(void)
{
// Declare Variables int code, day, month, year; float price;
// Prompt user for item code between 100 and 999 printf("Enter item code between 100 and 999: "); scanf("%d", &code);
// Print error if entry is not in the specified range if(code < 100 || code > 999) {
printf("error\n");
return 0;
}
// Prompt user for price between 0 and 9999.99 printf("Enter price between 0 and 9999.99: "); scanf("%f", &price);
// Print error if entry is not in the specified range if(price < 0 || price > 9999.99) {
printf("error\n");
return 0;
}
// Prompt user for valid purchase date printf("Enter purchase date (mm/dd/yyyy): "); scanf("%d/%d/%d", &month, &day, &year);
// Print error if entry is invalid if((month < 1 || month > 12) || (day < 1 || day > 31)|| (year < 1 || year > 9999)) { printf("error\n");
return 0;
}
// Display what the user entries in proper formatting printf("Code\tPrice\t\tPurchase Date\n"); printf("----\t--------\t------------\n"); printf("%d\t$%7.2f\t%.2d/%.2d/%.4d",
code, price, month, day, year);
return 0;
}
Output?
ReplyDelete