Test if input number is divisible by 3.
이전 댓글 표시
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
Chunru
2022년 8월 9일
doc input
doc mod
Walter Roberson
2022년 8월 9일
And watch out for the user entering things that are not numeric scalars.
Bozza
2022년 8월 9일
Chunru
2022년 8월 9일
In matlab command window, key in "doc input" or "doc mod" to see the detaild documentation and examples.
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일
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
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일
Walter Roberson
2022년 8월 9일
if y == 0
Review your code to find out what you have assigned to y
Bozza
2022년 8월 9일
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?
Bozza
2022년 8월 9일
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.
Hi @Bozza
I think it is called modular arithmetic in mathematics. But I did not learn this in high school. I remember the Math Teacher discussed something similar on the topic of Prime Numbers. Didn't understand why it is not taught.
Bozza
2022년 8월 9일
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일
1 개 추천
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
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!