how to find the summation for the odd and even numbers(from 0 to 100) save odd number in sum1 and even in sum2
조회 수: 58 (최근 30일)
이전 댓글 표시
using for loop not while
댓글 수: 4
Geoff Hayes
2019년 3월 10일
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).
답변 (1개)
Raghunandan V
2019년 3월 11일
편집: Raghunandan V
2019년 3월 12일
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
Geoff Hayes
2019년 3월 11일
The loop isn't necessary since k is unused and so you are just repeating the same four lines of code N times.
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!