- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Kotlin Program to Display Fibonacci Series
In this article, we will understand how to find even sum of Fibonacci series till number N. A Fibonacci series is sequence of numbers formed by the sum of its two previous integers. An even Fibonacci series is all the even numbers of the Fibonacci series.
A Fibonacci series till number N i.e., 10 can look like this −
0 1 1 2 3 5 8 13 21 34
Below is a demonstration of the same −
Suppose our input is −
The input : 15
The desired output would be −
The Fibonacci series till 15 terms: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Algorithm
Step 1 − START
Step 2 − Declare three variables namely myInput, temp1 and temp2
Step 3 − Define the values
Step 4 − Use a for loop to iterate through the integers from 1 to N and assign the sum of consequent two numbers as the current Fibonacci number
Step 5 − Display the result
Step 6 − Stop
Example 1
In this example, we will calculate and display the Fibonacci Series using for loop. First, declare and initialize variables for the input, and two temporary variables. These two variables temp1 and temp2 will store the 1st and 2nd values of a Fibonacci.
val myInput = 15 var temp1 = 0 var temp2 = 1
Now, get the Fibonacci using for loop −
for (i in 1..myInput) { print("$temp1 ") val sum = temp1 + temp2 temp1 = temp2 temp2 = sum }
Let us now display the Fibonacci Series −
fun main() { val myInput = 15 var temp1 = 0 var temp2 = 1 println("The number is defined as: $myInput") println("The Fibonacci series till $myInput terms:") for (i in 1..myInput) { print("$temp1 ") val sum = temp1 + temp2 temp1 = temp2 temp2 = sum } }
Output
The number is defined as: 15 The Fibonacci series till 15 terms: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Example 2
In this example, we will display a Fibonacci Series −
fun main() { val myInput = 15 println("The number is defined as: $myInput") fibonacciSeries(myInput) } fun fibonacciSeries(myInput: Int) { var temp1 = 0 var temp2 = 1 println("The fibonacci series till $myInput terms:") for (i in 1..myInput) { print("$temp1 ") val sum = temp1 + temp2 temp1 = temp2 temp2 = sum } }
Output
The number is defined as: 15 The fibonacci series till 15 terms: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
- Related Articles
- C++ Program to Display Fibonacci Series
- Java Program to Display Fibonacci Series
- Swift Program to Display Fibonacci Series
- Haskell Program to Display Fibonacci Series
- Java program to print a Fibonacci series
- Python Program to Display Fibonacci Sequence Using Recursion
- Fibonacci series program in Java using recursion.
- Write a Golang program to print the Fibonacci series
- Python Program to Find the Fibonacci Series Using Recursion
- Generate Fibonacci Series
- Java program to print Fibonacci series of a given number.
- C program to find Fibonacci series for a given number
- Python Program to Find the Fibonacci Series without Using Recursion
- Fibonacci series program in Java without using recursion.
- Program to find Fibonacci series results up to nth term in Python
