Change for loop to while loop.

I need to convert this code from the for loop to while loop but when I tried to do so, it always comes out as error. Please help :(
n = input ("Input a number: ");
fprintf("\nOdd number: ");
for i = 1:2:n
fprintf("%d ",i);
end
fprintf("\nEven number: ");
for i = 2:2:n
fprintf("%d ",i);
end
fprintf("\nSum of Even numbers: ");
sum = 0;
for i = 2:2:n
sum = sum + i;
end
fprintf("%d",sum);
prod = 1;
fprintf("\nProduct of Odd numbers: ");
for i = 1:2:n
prod = prod*i;
end
fprintf("%d",prod);

5 件のコメント

DGM
DGM 2022 年 4 月 24 日
編集済み: DGM 2022 年 4 月 24 日
Why? It would only require more code and introduce more opportunities for bugs. If your loop has a known number of iterations, use a for loop.
Cha Rot
Cha Rot 2022 年 4 月 24 日
It is the one required by our teacher as he wanted us to differentiate the for loop and while loop and prove that it will produce the same output.
The output should be like this. This is a sample output.
Input a number: 15
Odd Number: 1 3 5 7 9 11 13 15
Even Number: 2 4 6 8 10 12 14
Sum of Even Numbers: 56
Product of Odd Numbers: 2027025
Dyuman Joshi
Dyuman Joshi 2022 年 4 月 24 日
What have you tried? What error does it show?
Cha Rot
Cha Rot 2022 年 4 月 24 日
This is the code I tried.
n = input ("Input an integer: ");
fprintf('Odd numbers: ')
x = 1;
while x < n
x = 1:2:n;
fprintf(' %d',x)
break
end
fprintf('\nEven numbers:')
y = 0;
while y < n
y = 2:2:n;
fprintf(' %d',y)
break
end
fprintf('\nSum of even numbers:')
y = 0;
even_sum = 0;
while y < n
y = 2:2:n;
even_sum = even_sum + y;
fprintf(' %d', even_sum)
break
end
fprintf('\nProduct of odd numbers: ')
x = 1;
while x < n
x = 1:2:n;
odd_product = odd_product * x
fprintf(' %d',x)
break
end
But the last 2 parts, the sum of even numbers and product of odd numbers always produces the wrong outputs.
DGM
DGM 2022 年 4 月 24 日
Again:
fprintf('\nProduct of odd numbers: ')
x = 1;
odd_product = 1; % need to initialize to something nonzero
while x <= n % less than or equal to
odd_product = odd_product * x;
fprintf(' %d',odd_product) % not x
x = x+2; % increment x
end

サインインしてコメントする。

回答 (2 件)

Dyuman Joshi
Dyuman Joshi 2022 年 4 月 24 日

1 投票

%n = input ("Input an integer: ");
n=11;
fprintf('Odd numbers: ')
Odd numbers:
x=1;o=1;
while x<=n
fprintf(' %d',x)
o=o*x;
x=x+2;
end
1 3 5 7 9 11
fprintf('\nEven numbers:')
Even numbers:
y=0;e=0;
while y<=n
fprintf(' %d',y)
e=e+y;
y=y+2;
end
0 2 4 6 8 10
fprintf('\nSum of even numbers: %d', e)
Sum of even numbers: 30
fprintf('\nProduct of odd numbers: %d', o)
Product of odd numbers: 10395
DGM
DGM 2022 年 4 月 24 日

0 投票

If you want to make things more complicated, you can:
n = 5; %input ("Input a number: ");
fprintf("\nOdd number: ");
i = 1;
while i <= n
fprintf("%d ",i);
i = i+2;
end
% and so on

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2022 年 4 月 24 日

回答済み:

2022 年 4 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by