is the solution correct?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
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);
fprintf('Sum of even numbers: %d\n', sum_even);
fprintf('Count of odd numbers: %d\n', odd_count);
output:
Sum of all numbers: 154
Sum of even numbers: 134
Count of odd numbers: 3
1 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Biomechanics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!