Can not use the Fibonacci built in function

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... (add previous two numbers together for the next in the sequence)
1. Find the first 10 odd numbers.
2. The First 10 odd numbers are: <Provide all numbers here with a space between each>

7 Kommentare

James Tursa
James Tursa am 23 Sep. 2019
What have you done so far? What specific problems are you having with your code?
Hunter Steele
Hunter Steele am 23 Sep. 2019
i have calculated the total number, however I cant seem to figure out how to find the odd numbers and output just the odd numbers.
Walter Roberson
Walter Roberson am 23 Sep. 2019
hint: look at rem() and mod()
Hunter Steele
Hunter Steele am 24 Sep. 2019
I dont quite know which numbers to plug in for this problem anyway you could go into further detail?
James Tursa
James Tursa am 24 Sep. 2019
Bearbeitet: James Tursa am 24 Sep. 2019
Please post the code you have and point out what lines you are having problems with. Can you write code that will print all of the numbers (both even and odd) for e.g. the first 20 iterations?
Hunter Steele
Hunter Steele am 24 Sep. 2019
n = input('Enter10:');
b(1) = 0;
b(2) = 1;
x = 3;
while x <= n
b(x)=(b(x-1)+b(x-2));
x = x+1;
end
disp('The first 10 values of the Fibonacci are:')
disp(b)
Hunter Steele
Hunter Steele am 24 Sep. 2019
That gives me the numers for the firdt 10 values. I need to know how to get the first 10 odd values.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

James Tursa
James Tursa am 24 Sep. 2019
Bearbeitet: James Tursa am 24 Sep. 2019

0 Stimmen

For simplicity, let's assume you don't know how many iterations you need in advance. So start with your current code:
while x <= n
b(x)=(b(x-1)+b(x-2));
x = x+1;
end
and modify it to keep running until we get 10 odd numbers:
k = 1; % the number of odd numbers we have so far (starts at 1 because b(2) is an odd number)
while true
b(x)=(b(x-1)+b(x-2));
if( b(x) is an odd number ) % <-- you need to fill in the actual test for odd number here
k = k + 1; % increment the number of odd numbers we have so far
% do something here with the odd b(x)
if( k == n ) % if we have enough odd numbers, then break out of loop
break;
end
end
x = x+1;
end
So, try to fill in that test for the odd number. Then try to think of a way to either print the odd numbers as you go, or save the odd numbers for later printing.

Produkte

Gefragt:

am 23 Sep. 2019

Bearbeitet:

am 24 Sep. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by