is the solution correct?

조회 수: 3 (최근 30일)
Musaed
Musaed 2023년 5월 12일
댓글: Walter Roberson 2023년 5월 27일
for numbres 50 70 13 14 7 add all find sum for even find how many odd use one loop
sume=0;
sum=0;
for x=1:5
z = input ('z=')
sum=sum+z;
if mod(z,2)==03
sume=sume+z;
end
if mod(z,2)~=0
count0= count+1;
end
end
is the solution correct?
  댓글 수: 1
Torsten
Torsten 2023년 5월 12일
편집: Torsten 2023년 5월 12일
is the solution correct?
Easy to check by yourself:
Z=[50 70 13 14 7];
sume=0;
sum=0;
for x=1:5
z = Z(x);
sum=sum+z;
if mod(z,2)==03
sume=sume+z;
end
if mod(z,2)~=0
count0= count+1;
end
end
Error using count
Not enough input arguments.

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

답변 (1개)

Diwakar Diwakar
Diwakar Diwakar 2023년 5월 27일
편집: Diwakar Diwakar 2023년 5월 27일
numbers = [50, 70, 13, 14, 7];
sum_all = 0;
sum_even = 0;
odd_count = 0;
for i = 1:length(numbers)
num = numbers(i);
sum_all = sum_all + num; % Adding to the sum of all numbers
if mod(num, 2) == 0
sum_even = sum_even + num; % Adding to the sum of even numbers
else
odd_count = odd_count + 1; % Counting odd numbers
end
end
fprintf('Sum of all numbers: %d\n', sum_all);
Sum of all numbers: 154
fprintf('Sum of even numbers: %d\n', sum_even);
Sum of even numbers: 134
fprintf('Count of odd numbers: %d\n', odd_count);
Count of odd numbers: 2
output:
Sum of all numbers: 154
Sum of even numbers: 134
Count of odd numbers: 3
  댓글 수: 1
Walter Roberson
Walter Roberson 2023년 5월 27일
We recommend against providing complete solutions to homework problems.

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

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by