# 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)
Harsha Mude on 16 Aug 2020
Commented: Walter Roberson on 5 Jun 2021
I NEED THIS QUESTION ANSWER S = 1/2 N(N+1)

Sara Boznik on 16 Aug 2020
That is easy.
n=input('n:')
sum=0;
for i=1:n
sum=sum+i
end
##### 2 CommentsShowHide 1 older comment
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.

Mohammad Emaz Uddin on 3 Feb 2021
clc;
clear;
close all;
n= input('Enter numbers: ');
sum=0;
for i=1:1:n
sum=sum+i;
end
disp (sum);
ibraheem kanai on 5 Jun 2021
السلام عليكم ممكن تعرف حل المعادلات اذا امكن

Ghadeer Qaysar on 6 Apr 2021
Using a suitable loop, Write a MATLAB program to calculate the sum of first 7 natural numbers
##### 2 CommentsShowHide 1 older comment
Walter Roberson on 5 Jun 2021
N = randperm(7);
S = 0;
for J = 1 : length(N)
for K = 1 : length(N)
if N(J) == K
S = S + K;
end
end
end
S
S = 28