Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
Joe810
Joe810 le 18 Juin 2015
Commenté : Purushottama Rao le 19 Juin 2015
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

Réponses (2)

Azzi Abdelmalek
Azzi Abdelmalek le 18 Juin 2015
Modifié(e) : Azzi Abdelmalek le 18 Juin 2015
n=6
out=sum(1:n)

Purushottama Rao
Purushottama Rao le 19 Juin 2015
clc
k=input('Enter no');
sum=0;
for n=1:k
sum=sum+n;
end
disp(sum)
  1 commentaire
Purushottama Rao
Purushottama Rao le 19 Juin 2015
'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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by