現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
Test if input number is divisible by 3.
13 ビュー (過去 30 日間)
古いコメントを表示
Bozza
2022 年 8 月 9 日
I want to write a program to request a number from the user, and do one of two things:
- If the number is divisible by three, output that it is divisible by 3 and state the quotient.
- If the number is NOT divisible by three, state this, and report back the quotient and remainder.
18 件のコメント
Walter Roberson
2022 年 8 月 9 日
And watch out for the user entering things that are not numeric scalars.
Bozza
2022 年 8 月 9 日
I'm very new to MATLAB, so could you please explain how to use the doc input and doc mod functions?
Chunru
2022 年 8 月 9 日
In matlab command window, key in "doc input" or "doc mod" to see the detaild documentation and examples.
Bozza
2022 年 8 月 9 日
編集済み: Bozza
2022 年 8 月 9 日
I have done this so far, just can't seem to find how to display the things I want it to:
prompt = "Input a number:"
x = input(prompt)
%check x is whole positive number
if mod(x,1) == 0
disp('Your number is valid.')
elseif mod(x,1) ~= 0
disp('Your number is not valid')
%test if x is divisible by 3
divide=x/3
%mod function to find the remainder
remainder = mod(x,3)
%output
if ; disp('Your number is divisible by three, this many times: .')
elseif disp('Your number does not divide equally into 3, it can divide into three this many times: , with remainder: ')
Walter Roberson
2022 年 8 月 9 日
mod(x,1) == 0
That will calculate and display a result, but will not use that result to make any decision.
if ; disp('Your number is divisible by three, this many times: .')
You need to give a condition after the if
If remainder is what?
disp('Your number is divisible by three, this many times: .')
I recommend that you investigate fprintf(()
Bozza
2022 年 8 月 9 日
I have fixed a few things, however, I can't seem to get fprintf(() to work.
prompt = "Input a number:";
x = input(prompt);
%check x is whole positive number
y=mod(x,1);
if y == 0;
disp('Your number is valid.');
elseif y ~= 0;
disp('Your number is not valid');
%I want to make it so that if this elseif is true, it asks the question
%again
end
%test if x is divisible by 3
divide=x/3
%mod function to find the remainder
remainder = mod(x,3)
%output
if y == 0 fprintf('Your number is divisible by three, this many times: %d\n', divide && remainder);
elseif y ~= 0 fprintf('Your number does not divide equally into 3, it can divide into three this many times: %d\n', divide, 'with remainder: %d\n', remainder);
end
Rik
2022 年 8 月 9 日
If you want to repeat something an unknown number of times, you may want to consider a while loop.
Also, if you want something to happen in your else, you don't need to use elseif. In your code you test whether y is 0, and if it isn't you test whether y is not equal to 0. The only thing this case do is cause edge cases. You should alway have an else branch, even if you don't expect that to ever occur.
y=[0 1];
if y==0
disp('y is 0')
elseif y~=0
disp('y is not 0')
else
disp('y neither 0, nor not 0')
end
y neither 0, nor not 0
Walter Roberson
2022 年 8 月 9 日
if y == 0
fprintf('Your number is divisible by three, this many times: %d\n', divide);
else
fprintf('Your number does not divide equally into 3, it can divide into three this many times: %d with remainder: %d\n', divide, remainder);
end
Bozza
2022 年 8 月 9 日
Thank you all for the help so far. At the moment it sort of works, the problem is that it always states that the number is divisible by 3, even if it isn't. Is there something I can change the divide x/3 line to that finds the amount of times 3 equally divides into the number?
prompt = "Input a number:";
x = input(prompt);
%check x is whole positive number
y=mod(x,1);
if y == 0;
disp('Your number is valid.');
elseif y ~= 0;
disp('Your number is not valid');
%I want to make it so that if this elseif is true, it asks the question
%again
end
%test if x is divisible by 3
divide=x/3; %NEED TO CHANGE this to something that only finds the amount of times 3 equally divides into x.
%mod function to find the remainder
remainder = mod(x,3);
%output
if y == 0
fprintf('Your number is divisible by three, this many times: %d\n', divide);
else
fprintf('Your number does not divide equally into 3, it can divide into three this many times: %d with remainder: %d\n', divide, remainder);
end
Bozza
2022 年 8 月 9 日
My code works now, only problem is that the divide output is not a whole number. I want it to output the quotient and remainder as whole numbers. Is there a function that can do this?
prompt = "Input a number:";
x = input(prompt);
%check x is whole positive number
y=mod(x,3);
%test if x is divisible by 3
divide=x/3;
%mod function to find the remainder
remainder = mod(x,3);
%output
if y == 0
fprintf('Your number is divisible by three, this many times: %d\n', divide);
else
fprintf('Your number does not divide equally into 3, it can divide into three this many times: %d with remainder: %d\n', divide, remainder);
end
Walter Roberson
2022 年 8 月 9 日
%check x is whole positive number
y=mod(x,3);
Does that line test if x is a whole positive number?
Bjorn Gustavsson
2022 年 8 月 9 日
Check the help and documentation of the mod-function, look at the examples and test them to see how the function works and what it returns.
Bozza
2022 年 8 月 9 日
I think I just fixed it with some simple maths. Leaving something for a bit can make you realise how easy the solution is :) Thanks for the help everyone.
John D'Errico
2022 年 10 月 5 日
When you edit away all of your questions, you insult the people who spent the time answering those questions. You hurt answers, since then your question is completely useless for anyone else to ever learn from.
If you were going to delete the question, you should not be asking it in the first place.
回答 (1 件)
Bjorn Gustavsson
2022 年 8 月 9 日
Have a look at the help and documentation for while. Also from the question I guess that you're reasonably new to programming. If that's the case have a walk through the on-ramp material. That is designed to get you up to speed with programming, and supposedly no other way should be better than you browsing through that focusing on the parts that are of most interest and relevance to you.
HTH
参考
カテゴリ
Help Center および File Exchange で Spline Postprocessing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)