Write a MATLAB script file that calculates the sum of first n natural numbers (the program could prompt the user for the number n). The program must use a “for” or “while” loop to accomplish this, and must NOT use the ready formula s = 1 2 n (n + 1).

942 views (last 30 days)
I NEED THIS QUESTION ANSWER S = 1/2 N(N+1)

Accepted Answer

Sara Boznik
Sara Boznik on 16 Aug 2020
That is easy.
n=input('n:')
sum=0;
for i=1:n
sum=sum+i
end
  2 Comments
Sara Boznik
Sara Boznik on 16 Aug 2020
yes, I usually use the name in my own language. so I dont have trouble with function name. I used sum in this case that Harsha will understand what the coda means.

Sign in to comment.

More Answers (2)


Ghadeer Qaysar
Ghadeer Qaysar on 6 Apr 2021
Using a suitable loop, Write a MATLAB program to calculate the sum of first 7 natural numbers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by