MATLAB: How do i create a code which calculates the sum of number from 1 to x

sum of positive integers from 1 to an unknown

Create a function, which calculates the sum of numbers from 1 to x. You will need to use a while loop, Also how could i Add to the code so that the function also displays if the number is odd or even PS using hole integers

Best Answer

function y=whileSum(x)
sum=0;count=0;
while count<=x
sum=sum+count
count=count+1;
end
y=sum;
and call it from command line as
whileSum(9)
and the result will be
45
Related Question