w3resource

R Programming: Get the first 10 Fibonacci numbers

R Programming: Basic Exercise-5 with Solution

Write a R program to get the first 10 Fibonacci numbers.

Sample Solution :

R Programming Code :

Fibonacci <- numeric(10)
Fibonacci[1] <- Fibonacci[2] <- 1
for (i in 3:10) Fibonacci[i] <- Fibonacci[i - 2] + Fibonacci[i - 1]
print("First 10 Fibonacci numbers:")
print(Fibonacci)

Sample Output:

[1] "First 10 Fibonacci numbers:"
 [1]  1  1  2  3  5  8 13 21 34 55                         

R Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a R program to create a vector which contains 10 random integer values between -50 and +50.
Next: Write a R program to get all prime numbers up to a given number (based on the sieve of Eratosthenes).

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.