How do I make a for loop for summing up to specified integer
1 次查看(过去 30 天)
显示 更早的评论
How can I make a for loop for summing up integers starting at 0 and going to the integer the user inputs(user can choose any integer)
ex:
user inputs 3: 1+2+3
user inputs 6: 1+2+3+4+5+6
0 个评论
回答(2 个)
Azzi Abdelmalek
2015-6-18
编辑:Azzi Abdelmalek
2015-6-18
n=6
out=sum(1:n)
Purushottama Rao
2015-6-19
clc
k=input('Enter no');
sum=0;
for n=1:k
sum=sum+n;
end
disp(sum)
1 个评论
Purushottama Rao
2015-6-19
'For' loop is not necessary for whay you wanted to do. You can rather use input function along with sum. However since you were specific to use for loop, above code is given
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!