How do I make a for loop for summing up to specified integer

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

답변 (2개)

Purushottama Rao
Purushottama Rao 2015년 6월 19일
clc
k=input('Enter no');
sum=0;
for n=1:k
sum=sum+n;
end
disp(sum)

댓글 수: 1

'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

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2015년 6월 18일

댓글:

2015년 6월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by