Write a program in MATLAB that inputs a date and outputs the day of the week for
ID: 3561969 • Letter: W
Question
Write a program in MATLAB that inputs a date and outputs the day of the week for that date. The program should work as follows:
Enter the year (1801 .. 2199) > 2007
Enter the month number (January =1) > 1
Enter the day (1 .. 31) > 25
Thursday
The program should work for dates form January 1, 1801 to December 31, 2199. You do not have to reject values like February 31, 2007 which are potentially legal values but do not represent actual dates.
In the program you may assume Year, Month, and Day are the input values with January =1, February =2, etc. Calculate the value of variable D that will contain the day of the week, encoded with Sunday =1, Monday=2, etc.
Explanation / Answer
year=input('Enter the year (1801 .. 2199)>');
month=input('Enter the month number (January=1)>');
day=input('Enter the day (1 .. 31)>');
c=calendar(year,month);
[i,j]=find(c==day);
if j==1
fprintf('Sunday ');
elseif j==2
fprintf('Monday ');
elseif j==3
fprintf('Tuesday ');
elseif j==4
fprintf('Wednesday ');
elseif j==5
fprintf('Thursday ');
elseif j==6
fprintf('Friday ');
elseif j==7
fprintf('Saturday ');
else
fprintf('Invalid input ');
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.