Month/Day Code#include <stdio.h>
#include <stdlib.h> int main() { int month; do{ printf("Enter number of month: "); scanf("%d",&month); switch(month){ case 1: printf("There are 31 days in January.\n"); break; case 2: printf("There are 28 days in February\n"); break; case 3: printf("There are 31 days in March\n"); break; case 4: printf("There are 30 days in April\n"); break; case 5: printf("There are 31 days in May\n"); break; case 6: printf("There are 30 days in June\n"); break; case 7: printf("There are 31 days in July\n"); break; case 8: printf("There are 31 days in August\n"); break; case 9: printf("There are 30 days in September\n"); break; case 10: printf("There are 31 days in October\n"); break; case 11: printf("There are 30 days in November\n"); break; case 12: printf("There are 31 days in December\n"); break; default:printf("Not a valid answer.\n"); } } while(month <1 || month >12); printf("Thank you for using the month calculator!\n"); return 0; } |
Month/Day DetailsThis code uses a case/switch system. The case system is kinda of like a if statement system. The code asks the user to enter the number of a month, 1-12. The code then prints the case that corresponds to the number. Each case has a month and the number of days within that month. If the user enters a number or character besides 1-12 the switch will print the default ," Not a valid number." The default acts like an else statement.
Real Time ApplicationsThis code could be used in a game, inventory type system, or to record different events to a number or year.
|