While Loop Consecutive Odd Integers Question

Starting from 100, subtract consecutive integer odd numbers (1, 3, 5, …) until you reach 0 or a
negative number. Count how many integer odd numbers you used.
The sequence would be:
100 – 1
99 – 3
96 – 5
91 – 7
84 – 9
75 – 11
Until the left column reaches 0 or a negative number.
I need to do this but using a while loop and not a for loop and I am just a bit stuck.

7 件のコメント

madhan ravi
madhan ravi 2020 年 7 月 2 日
What did you do for your homework?
Adelyn Adams
Adelyn Adams 2020 年 7 月 2 日
What do you mean?
Steven Lord
Steven Lord 2020 年 7 月 2 日
This sounds like a homework assignment. If it is, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the MATLAB Onramp tutorial (https://www.mathworks.com/support/learn-with-matlab-tutorials.html) to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.
Adelyn Adams
Adelyn Adams 2020 年 7 月 2 日
No I am not looking for a solution to this, I just need to know if there is a way to create like a vector or something that can produce consecutive integer odd numbers.
Adelyn Adams
Adelyn Adams 2020 年 7 月 2 日
Also, this is not a homework question, I am just practicing Matlab
Walter Roberson
Walter Roberson 2020 年 7 月 2 日
1:2:20 for example. However, you do not know ahead of time how many of them you will need. You can put an upper bound on how many you will need -- you know that you will never need more than 100 of them.
It is possible to calculate the exact solution mathematically, by the way.

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

回答 (1 件)

Rohith Nomula
Rohith Nomula 2020 年 7 月 3 日

0 投票

This can be done using a while loop
Initialize i from 100 and j from 1. Here the one condition is i should be greater that 0
count increases as you use up one odd integer to subtract from the initial integer
i=100;
j=1;
count=0;
while i>0
count=count+1;
i=i-j;
j=j+2;
disp(i);
end
disp(count);
Try this !

2 件のコメント

madhan ravi
madhan ravi 2020 年 7 月 3 日
編集済み: madhan ravi 2020 年 7 月 3 日
Solving a complete homework problem is highly discouraged in this forum, rather steps explaining the step would be intuitive and stimulating to learn more.
Rohith Nomula
Rohith Nomula 2020 年 7 月 3 日
Adelyn said its not a homework question

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

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

質問済み:

2020 年 7 月 2 日

編集済み:

2020 年 7 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by