Fibonacci series using while loop
In this post, you will learn how to find the Fibonacci series using the while loop.
The Fibonacci series is the sequence of numbers in which the next number is the sum of the two previous numbers. The Fibonacci series was well-known hundreds of years earlier. The "Fibonacci" name came from the nickname "Bonacci".
We can easily remember the Fibonacci sequence using the Fibonacci Day, which is November 23rd. As 23rd November has the digits "1, 1, 2, 3" which is part of the sequence.
Fibonacci Series Program in PHP using while loop
In the given PHP program, we have used the while loop to print the Fibonacci series upto 15 in PHP. First, we print the first and second numbers. Then we use the while loop, where we get the next number by adding the previous two numbers and simultaneously swap the first number with the second and the second with the third.
<?php
$n = 0;
$a = 0;
$b = 1;
echo "Fibonacci series upto 15 : ";
echo "$a, $b";
while ($n < 16 )
{
$c = $b + $a;
echo ", ";
echo "$c";
$a = $b;
$b = $c;
$n = $n + 1;
}
?>
Output of the above code:
Fibonacci series upto 15 : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597
Python program to find the Fibonacci Series using while loop
In this Python program, we have used the while loop to find the Fibonacci Series numbers within that range.
num = int(input("Enter the Fibonacci Number Range: "))
n1 = 0
n2 = 1
Sum = 0
i = 0
while(i < num):
print(n1, end = ' ')
Sum = Sum + n1
Next = n1 + n2
n1 = n2
n2 = Next
i = i + 1
print("\nSum of Fibonacci Series Numbers: %d" %Sum)
Output of the above code:
Enter the Fibonacci Number Range: 12
0 1 1 2 3 5 8 13 21 34 55 89
Sum of Fibonacci Series Numbers: 232
Fibonacci series program in Java using while loop
Here, you will learn how to print the Fibonacci series using the while loop in the Java programming language. In the below code, n1 contains the first number, i.e., 0 and n2 contains the second number, i.e., 1 and n contains the total Fibonacci series number count. In each iteration of the while loop, we get the next number by adding the previous two numbers, and simultaneously, we swap the first number with the second and the second with the third.
public class FibonacciSeries{
static void fibonacciSeries(int n)
{
int n1 = 0, n2 = 1;
int counter = 0;
while (counter < n) {
// Print the number
System.out.print(n1 + " ");
int n3 = n2 + n1;
n1 = n2;
n2 = n3;
counter = counter + 1;
}
}
// Driver Code
public static void main(String args[])
{
// initialize number n
int n = 13;
// Call function
fibonacciSeries(n);
}
}
Output of the above code:
0 1 1 2 3 5 8 13 21 34 55 89 144
Related Articles
Python program to print all even numbers between 1 to 100
Pandas DataFrame to JSON
Prettytable in Python
Count consonants in a string Python
Python split strings by comma
Count vowels in a string Python
Replace multiple characters Python
Convert array to list Python
Python loop through list
Difference between tuple and list in Python
Convert string to list Python
Remove last element from list Python
Convert list to string Python
Convert Python list to numpy array
Remove element from list Python
Python dict inside list
Check if list is empty Python
Python iterate list with index
Python iterate list with index
Python split string by comma
Printing Simple Diamond Pattern in Python
Stemming and Lemmatization in Python
Python | Generate QR Code using pyqrcode module