Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program 1: (Sum of Even Numbers): Using for loop, write a function named evenNum

ID: 3804660 • Letter: P

Question

Program 1: (Sum of Even Numbers): Using for loop, write a function named evenNumberAdder that takes a number n as input and outputs the sum of all even numbers 0+2+4+6+…+n. If the number is negative, you programs prints out an error message and returns 0. (11 points)
   Example: evenNumberAdder (-2) ’Invalid number’
       evenNumberAdder (12) 42
Bonus: Write an oddNumberAdder to calculate the sum of odd numbers up to n.

PLEASE I WANT THIS PROGRAM TO BE WRITTEN IN MATLAB SO THAT IT WILL RUN CORRECTLY

PLEASE GIVE ME AN ANSWER THAT NO ONE ELSE HAS WRITTEN BEFORE

Explanation / Answer

function y=evenNumberAdder(N)
if (N<0) %if negative returns 0
disp('Invalid number')
sum=0;
end
sum=0;
for x= (0:N) //sum all evenss from 0 to N inclusive
       if (mod(x,2) == 0)
           sum=sum+x;
%disp(x);
end
end
fprintf('Sum of even numbers is %d ',sum)
end
N=input(' Enter a number =');
evenNumberAdder(N) % calling the function to add evens

%%%%%%%%%%%%%%%%%%%%%Odd Number Adder%%%%%%

function y=oddNumberAdder(N)
if (N<0)
disp('Invalid number')
sum=0;
end
sum=0;
for x= (0:N)
       if (mod(x,2) != 0)
           sum=sum+x;
%disp(x);
end
end
fprintf('Sum of odd numbers is %d ',sum)
end
N=input(' Enter a number =');
oddNumberAdder(N)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote