Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

# Program to get Fibonacci sequence

## Q. What iS Fibonacci sequence ?

The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones. The sequence starts with 0 and 1, and each subsequent number is obtained by adding the two numbers that came before it.

# Program in C

C
``````#include <stdio.h>

void fibonacci(int num);

int main() {
int num;

printf("Enter the number of terms: ");
scanf("%d", &num);

printf("Fibonacci Sequence up to %d:\n", num);
fibonacci(num);

return 0;
}

void fibonacci(int num) {
int first = 0, second = 1, next;

printf("%d ", first);
printf("%d ", second);

for (int i = 3; i <= num; i++) {
next = first + second;
printf("%d ", next);

first = second;
second = next;
}

printf("\n");
}``````

## Explanation:

• In this program, we define a function fibonacci that takes the input number (num) as an argument and prints the Fibonacci sequence up to that number.
• The main function prompts the user to enter the number of terms they want to generate and reads it from the standard input using scanf.
• The program then calls the fibonacci function with the entered number as an argument.
• Inside the fibonacci function, we initialize three variables: first (initialized to 0), second (initialized to 1), and next. We print the first two terms of the sequence (0 and 1).
• Next, we use a for loop starting from 3 (since we already printed the first two terms) up to the entered number (num).
• In each iteration, we calculate the next term by adding first and second, assign it to next, and print it.
• Then, we update first and second to prepare for the next iteration.
• Finally, we print a newline character to move to the next line after printing the entire sequence.

## Output:

Output
``````Enter the number of terms: 8
Fibonacci Sequence up to 8:
0 1 1 2 3 5 8 13 ``````

# Program in Java

Java
``````import java.util.Scanner;

public class FibonacciJava {
public static void fibonacci(int num) {
int first = 0, second = 1, next;

System.out.print(first + " ");
System.out.print(second + " ");

for (int i = 3; i <= num; i++) {
next = first + second;
System.out.print(next + " ");

first = second;
second = next;
}

System.out.println();
}

public static void main(String[] args) {
int num;

System.out.print("Enter the number of terms: ");
Scanner scanner = new Scanner(System.in);
num = scanner.nextInt();

System.out.println("Fibonacci Sequence up to " + num + ":");
fibonacci(num);

scanner.close();
}
}``````

## Explanation:

• import java.util.Scanner;: This line imports the Scanner class from the java.util package, allowing us to read user input from the console.
• public class FibonacciJava: This line defines a public class named FibonacciJava.
• public static void fibonacci(int num): This line declares a static method fibonacci that takes an integer parameter num. This method calculates and prints the Fibonacci sequence up to the given num.
• int first = 0, second = 1, next;: This line declares three integer variables, first, second, and next. first and second represent the first and second terms of the Fibonacci sequence, respectively. next will store the next term in the sequence.
• System.out.print(first + ” “);: This line prints the value of first followed by a space. It represents the first term of the Fibonacci sequence.
• System.out.print(second + ” “);: This line prints the value of second followed by a space. It represents the second term of the Fibonacci sequence.
• The for loop calculates and prints the subsequent terms of the Fibonacci sequence.
• int i = 3; declares and initializes the loop counter i to 3.
• i <= num; specifies the loop condition, which continues until i is less than or equal to num.
• next = first + second; calculates the next term of the Fibonacci sequence by adding first and second.
• System.out.print(next + ” “); prints the value of next followed by a space.
• first = second; assigns the value of second to first for the next iteration.
• second = next; assigns the value of next to second for the next iteration.
• System.out.println();: This line prints a newline character, indicating the end of the Fibonacci sequence.
• The main method serves as the entry point of the program.
• int num; declares an integer variable num to store the user input.
• System.out.print(“Enter the number of terms: “); prompts the user to enter the number of terms.
• Scanner scanner = new Scanner(System.in); creates a Scanner object named scanner to read user input from the console.
• num = scanner.nextInt(); reads an integer from the user and assigns it to the variable num.
• System.out.println(“Fibonacci Sequence up to ” + num + “:”); prints the message indicating the Fibonacci sequence calculation is starting.
• fibonacci(num); calls the fibonacci method to calculate and print the Fibonacci sequence up to the specified number of terms.
• scanner.close(); closes the Scanner object to release system resources.
Output
``````Enter the number of terms: 8
Fibonacci Sequence up to 8:
0 1 1 2 3 5 8 13 ``````