Effacer les filtres
Effacer les filtres

how to find the summation for the odd and even numbers(from 0 to 100) save odd number in sum1 and even in sum2

30 vues (au cours des 30 derniers jours)
using for loop not while
  4 commentaires
Geoff Hayes
Geoff Hayes le 10 Mar 2019
yazan - we have all been new to MATLAB at one time...but that shouldn't be a reason to not try do your homework. Please make an attempt at coding up a solution for this problem. Since you have to use a for loop (and not a while loop) see for loop and mod (which can be used to determine if a number is even or odd).

Connectez-vous pour commenter.

Réponses (1)

Raghunandan V
Raghunandan V le 11 Mar 2019
Modifié(e) : Raghunandan V le 12 Mar 2019
tryThis:
N=100;
Numbers = [1:1:N];
Even = Numbers((mod(Numbers,2) ==0));
Sum1 = sum(Even);
odd = Numbers((mod(Numbers,2) ==1));
sum2 = sum(odd);
  2 commentaires
Geoff Hayes
Geoff Hayes le 11 Mar 2019
The loop isn't necessary since k is unused and so you are just repeating the same four lines of code N times.
Raghunandan V
Raghunandan V le 12 Mar 2019
Yes, you are right. I have updated the answer. thanks for the input

Connectez-vous pour commenter.

Catégories

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

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by