I got most of it except how to put 'is a multiple of three' after the multiples
4 views (last 30 days)
Show older comments
The Fibonacci sequence starts with the numbers 1, 1, 2, 3, ... and continues as the next number of the sequence is the sum of the pr evious two numbers. Write the script (m - file) to list the sequence vertically in the command window up to the 20 th number and indicate which numbers are a multiple of 3
0 Comments
Answers (1)
Image Analyst
on 11 Jul 2017
Edited: Image Analyst
on 11 Jul 2017
Hint: try checking if mod(yourNumber, 3) or rem(yourNumber, 3) is zero:
if rem(yourNumber, 3) == 0
% It's a multiple of 3
else
% It's not a multiple of 3
end
Note: yourNumber could also be the array of ALL the numbers if you want, then
multiplesOf3 = find(rem(yourNumbers, 3) == 0)
etc. multiplesOf3 will be a list of indexes of elements that are a multiple of 3.
2 Comments
Image Analyst
on 11 Jul 2017
Do you know about fprintf()? In your loop:
if rem(yourNumber, 3) == 0
% It's a multiple of 3. Print number and text.
fprintf('%d is a multiple of three\n', yourNumber);
else
% It's not a multiple of 3. Print number alone.
fprintf('%d\n', yourNumber);
end
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!