Thanks to visit codestin.com
Credit goes to www.rameshfadatare.com

R Program to Print the Fibonacci Sequence

Introduction

The Fibonacci sequence is a series of numbers where each number (after the first two) is the sum of the two preceding ones, usually starting with 0 and 1. This guide will walk you through writing an R program that prints the Fibonacci sequence up to a specified number of terms.

Problem Statement

Create an R program that:

  • Prompts the user to enter the number of terms for the Fibonacci sequence.
  • Generates and prints the Fibonacci sequence up to the specified number of terms.

Example:

  • Input: 7
  • Output: 0, 1, 1, 2, 3, 5, 8

Solution Steps

  1. Read the Number of Terms: Use the readline() function to take the number of terms as input from the user.
  2. Convert the Input to Numeric: Convert the input from a character string to a numeric value using the as.numeric() function.
  3. Generate the Fibonacci Sequence: Use a loop to generate the Fibonacci sequence up to the specified number of terms.
  4. Display the Fibonacci Sequence: Use the print() function to display the generated sequence.

R Program

# R Program to Print the Fibonacci Sequence
# Author: Ramesh Fadatare

# Step 1: Read the number of terms from the user
n_terms <- as.numeric(readline(prompt = "Enter the number of terms: "))

# Step 2: Initialize the first two terms of the Fibonacci sequence
fib_sequence <- numeric(n_terms)
fib_sequence[1] <- 0

if (n_terms > 1) {
  fib_sequence[2] <- 1
  
  # Step 3: Generate the Fibonacci sequence
  for (i in 3:n_terms) {
    fib_sequence[i] <- fib_sequence[i - 1] + fib_sequence[i - 2]
  }
}

# Step 4: Display the Fibonacci sequence
print(paste("Fibonacci sequence up to", n_terms, "terms:"))
print(fib_sequence)

Explanation

Step 1: Read the Number of Terms

  • The readline() function prompts the user to enter the number of terms for the Fibonacci sequence. The input is read as a string and converted to a numeric value using as.numeric().

Step 2: Initialize the First Two Terms

  • The Fibonacci sequence starts with 0 and 1. If the user enters a number of terms greater than 1, the first two terms are initialized in the fib_sequence vector.

Step 3: Generate the Fibonacci Sequence

  • A loop is used to generate the Fibonacci sequence from the 3rd term onwards. Each term is the sum of the two preceding terms.

Step 4: Display the Fibonacci Sequence

  • The print() function is used to display the entire Fibonacci sequence.

Output Example

Example:

Enter the number of terms: 7
[1] "Fibonacci sequence up to 7 terms:"
[1] 0 1 1 2 3 5 8

Conclusion

This R program demonstrates how to generate and print the Fibonacci sequence for a given number of terms. It covers basic concepts such as loops, vector manipulation, and user input, making it a practical example for beginners learning R programming. The Fibonacci sequence is a well-known mathematical series, and understanding how to implement it in R is a valuable skill.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top