FIBONACCI SERIES

************************************************************************ 

Write a c program to print Fibonacci series without using recursion and using recursion.

Input: 10

Output: 0 1 1 2 3 5 8 13 21 34

************************************************************************ 

JAVA Program

//import java.util.Date;
import java.util.Scanner;
public class HelloWorld {
    public static void main(String[] args) {
        // Date now = new Date();
        Scanner sc= new Scanner(System.in);
        int x,y,z,n,i;
        y=z=0;
        x=1;
        System.out.println("Enter value of n: ");
        n=sc.nextInt();
        System.out.println("FIBONACCI SERIES: " );
        for(i=0;i<n;i++)
        {
          System.out.print(" "+z);
          z=x+y;
          x=y;
          y=z;
        }
    }
}         

Online compiler

Output:

Input: 10

Output: 0 1 1 2 3 5 8 13 21 34

************************************************************************ 

C Program

#include<stdio.h>
// #include<conio.h>

int main()
{
  int x,y,z,n,i;
  y=z=0;
  x=1;
  printf("Enter value of n: ");
  scanf("%d",&n);
 
  printf("\nFIBONACCI SERIES: " );
  for(i=0;i<n;i++)
  {
    printf(" %d ",z);
    z=x+y;
    x=y;
    y=z;
  }
return 0;
getch();
}
       
Online Compiler 

Output

Input: 10

Output: 0 1 1 2 3 5 8 13 21 34


************************************************************************ 

C++ Program

#include <iostream>
using namespace std;

int main()
{
  int x,y,z,n,i;
  y=z=0;
  x=1;
  cout <<"Enter value of n: ";
  cin>>n;
  cout <<"\nFIBONACCI SERIES: ";
  for(i=0;i<n;i++)
  {
    cout <<z<<" ";
    z=x+y;
    x=y;
    y=z;
  }
return 0;

}
       
Online Compiler

Output

Input: 10

Output: 0 1 1 2 3 5 8 13 21 34

************************************************************************ 

PHP Program

<?php
    echo "Hello, world!!!!";
     // int x,y,z,n,i;
  $y=0;
  $z=0;
  $x=1;
  $a = (int)readline('Enter value of n: ');
  echo "\nFIBONACCI SERIES: ";
  for ($i = 0; $i < $a; $i++) {
 
    echo "$z ";
    $z=$x+$y;
    $x=$y;
    $y=$z;
}
 
?>

Online Compiler

Output: 

Input: 10

Output: 0 1 1 2 3 5 8 13 21 34

************************************************************************ 

JAVASCRIPT Program


Online Compiler

Output: 

Input: 10

Output: 0 1 1 2 3 5 8 13 21 34

************************************************************************ 

PYTHON Program


Output:

Input: 10

Output: 0 1 1 2 3 5 8 13 21 34

Online Compiler: 

 

************************************************************************  


Comments

Popular posts from this blog

SAME NAME IN CLASS METHOD AND CONSTRUCTOR

PALINDROME NUMBER