This solution is outdated. To rescore this solution, sign in.
-
2 Comments
Hi All,
Can anyone tell me what's wrong with this code?
function t = triangle(n)
sum=0:
for i=1:n
sum=sum+i
end
t =sum ;
end
I checked this code with test cases as n=1,3,5,30 in my MATLAB and I'm getting the desired result.
n=input("Enter a number")
sum=0;
for i=1:n
sum=sum+i
end
t =sum
your triangle function has the statement sum = 0:
your test code has the statement sum = 0;
you have a typo, : versus ;
:-)
function t = triangle(n)
sum=0;
for i=1:n
sum=sum+i;
end
t =sum;
end
PS better code would be
function t = triangle(n)
t = sum(1:n);
end
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!