Question:
How to determine number of days in a month of an year (not a leap year) in MATLAB?
Answer:
Number of days can be determined by using the rules:
1. If month number is 2, then number of days is 28.
2. If month number is 4,6,9 or 11, then number of days is 30.
3. If the month number is anything else the number of days is 31
4. If number is less than 1 and greater than 12, generate error message
i = input('Enter the Month Number e.g., 1-January, 2-February:'); if i < 1 | i > 12 disp('Numnber may be less than 1 or greater than 12! Enter Valid Number'); else if i == 2 fprintf('The Month Number is %d and number of days in this month are: %d ',i,28) elseif i == 4 | i == 6 | i == 9 | i == 11 fprintf('The Month Number is %d and number of days in this month are: %d ',i,30) else fprintf('The Month Number is %d and number of days in this month are: %d ',i,31) end end
OUTPUT
ATTEMPT I:
Enter the Month Number e.g., 1-January, 2-February: 3 The Month Number is 3 and number of days in this month are: 31
ATTEMPT II:
Enter the Month Number e.g., 1-January, 2-February: 11 The Month Number is 11 and number of days in this month are: 30
0 Comments