In: Mechanical Engineering
Use Matlab to write the following
1. Write code to determine whether a year is a leap year. Use the mod function. The rules for determining leap years in the Gregorian calendar are as follows:
All years evenly divisible by 400 are leap years.
Years evenly divisible by 100, but not by 400, are not leap years.
Years divisible by 4, but not by 100, are leap years.
All other years are not leap years.
For example, the years 1800, 1900, 2100, 2300 and 2500 are not leap years, but 2400 is a leap year.
2. Solve the following:
a. Use a for loop to determine the sum of the first 10 terms in the series 5k3, where k=1,2,3,...,10. Report your answer to the Command Window.
b. Repeat part (a) without using a loop.
Problem 1
MatLab program:
clear
clc
% year number
year=input('Enter the year: ');
if mod(year,400)==0
fprintf('\n%d is leap year',year);
elseif mod(year,100)==0
fprintf('\n%d is not a leap year',year);
elseif mod(year,4)==0
fprintf('\n%d is leap year',year);
else
fprintf('\n%d is not a leap year',year);
end
Screenshot:
Save the above program and execute it and give the input.
Give the input year
Result:
Problem 2(a)
MatLab program:
clear
clc
% initially sum of series is zero
sum_series=0;
% k=1 to 10
% for loop
for k=1:10
num=500+k*10+3;
sum_series=sum_series+num;
end
fprintf('sum of series is %d',sum_series);
Screenshot:
Save the above program and execute it.
Result:
Problem 2(b)
MatLab program:
clear
clc
% k=1 to 10
k=1:10;
% without for loop
num=500+k*10+3;
sum_series=sum(num);
fprintf('sum of series is %d',sum_series);
Screenshot:
Save the above program and execute it.
Result: