I got most of it except how to put 'is a multiple of three' after the multiples

4 views (last 30 days)
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

Answers (1)

Image Analyst
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
Irene Zhou
Irene Zhou on 11 Jul 2017
I got that but it is asked to be displayed in the format below
1
1
2
3 is a multiple of three
5
8
Image Analyst
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

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!