Program to Print Fibonacci Series using For loop
- Write a program to Print Fibonacci Series using For loop in C
- Write a program to Print Fibonacci Series using For loop in C++
- Write a program to Print Fibonacci Series using For loop in Python
- Write a program to Print Fibonacci Series using For loop in PHP
- Write a program to Print Fibonacci Series using For loop in Java
- Write a program to Print Fibonacci Series using For loop in Java Script
- Write a program to Print Fibonacci Series using For loop in C#
Explanation:
We begin with two numbers, 0 and 1, and then add the final two numbers in the sequence to produce successive numbers in order to display the Fibonacci series using a for loop.
Logic
- The first two numbers of the Fibonacci series are 0 and 1.
- Use a for loop to calculate the next number in the series by adding the two previous numbers.
- Update the previous numbers (a and b) after calculating each new number.
- Repeat until the desired number of terms is generated.
Program to Print Fibonacci Series using For loop
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h> int main() { int n; printf("Enter the number of terms in Fibonacci series: "); scanf("%d", &n); int first = 0, second = 1, next; printf("Fibonacci Series: "); for (int i = 1; i <= n; i++) { printf("%d ", first); next = first + second; first = second; second = next; } printf("\n"); return 0; }
#include <iostream> using namespace std; int main() { int n; cout << "Enter the number of terms in Fibonacci series: "; cin >> n; int first = 0, second = 1, next; cout << "Fibonacci Series: "; for (int i = 1; i <= n; i++) { cout << first << " "; next = first + second; first = second; second = next; } cout << endl; return 0; }
n = int(input("Enter the number of terms in Fibonacci series: ")) first, second = 0, 1 print("Fibonacci Series: ", end="") for _ in range(n): print(first, end=" ") first, second = second, first + second print()
<?php $n = (int)readline("Enter the number of terms in Fibonacci series: "); $first = 0; $second = 1; echo "Fibonacci Series: "; for ($i = 1; $i <= $n; $i++) { echo "$first "; $next = $first + $second; $first = $second; $second = $next; } echo "\n"; ?>
import java.util.Scanner; public class Fibonacci { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the number of terms in Fibonacci series: "); int n = scanner.nextInt(); int first = 0, second = 1, next; System.out.print("Fibonacci Series: "); for (int i = 1; i <= n; i++) { System.out.print(first + " "); next = first + second; first = second; second = next; } scanner.close(); System.out.println(); } }
let n = parseInt(prompt("Enter the number of terms in Fibonacci series:")); let first = 0, second = 1, next; let output = "Fibonacci Series: "; for (let i = 1; i <= n; i++) { output += first + " "; next = first + second; first = second; second = next; } console.log(output);
using System; class Program { static void Main() { Console.Write("Enter the number of terms in Fibonacci series: "); int n = int.Parse(Console.ReadLine()); int first = 0, second = 1, next; Console.Write("Fibonacci Series: "); for (int i = 1; i <= n; i++) { Console.Write(first + " "); next = first + second; first = second; second = next; } Console.WriteLine(); } }