Coding Question 2 Free Section

Fibonacci Series

C

#include <stdio.h>
int main()
{
    int i, n, t1 = 0, t2 = 1, nextTerm;

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

    printf("Fibonacci Series: ");

    for (i = 1; i <= n; ++i)
    {
        printf("%d, ", t1);
        nextTerm = t1 + t2;
        t1 = t2;
        t2 = nextTerm;
    }
    return 0;
}

Java

public class Fibonacci {

    public static void main(String[] args) {

        int n = 10, t1 = 0, t2 = 1;
        System.out.print("First " + n + " terms: ");

        for (int i = 1; i <= n; ++i)
        {
            System.out.print(t1 + " + ");

            int sum = t1 + t2;
            t1 = t2;
            t2 = sum;
        }
    }
}

6 comments on “Coding Question 2 Free Section”


  • Piyush

    num = int(input(“enter num ::”))
    a= 0;b= 1

    print(a,end=” “)
    while(not(b > num+1)):
    print(b,end= ” “)
    temp = a+b
    a= b
    b = temp


  • raghvendra

    in Python
    n=int(input(“Enter the number”))
    first=0
    second=1
    for i in range(n):
    print(first)
    temp=first
    first=second
    second=temp+second


  • sai

    #include
    int main()
    {
    int a=0;
    int b=1;
    int c=0;
    int n,i;
    printf(“enter n value”);
    scanf(“%d”,&n);
    printf(“%d%d”,a,b);
    while(i<=n)
    {
    c=a+b;
    printf("%d",c);
    a=b;
    b=c;
    i++;
    }
    return 0;
    }


  • monika baskey

    IN Python
    def fibonacci(n):
    if(n<0):
    print(false)
    if(n==0):
    return 0
    if(n==0):
    return 1
    else:
    return(fibonacci(n-2)+fibonacci(n-1)
    fibonacci(9)
    if(fibonacci(n)):
    print("fibonacci of",n,'is':fibonacci(n))
    else:
    print("Error")


  • Shubhrajit Dey

    it will print the fibo series
    import java.util.*;
    public class Fibonacci{
    static int fibo(int f){
    if(f<=1){
    return f;
    }
    else{
    return fibo(f-1)+fibo(f-2);
    }
    }
    public static void main(String args[]){
    Scanner sc=new Scanner(System.in);
    System.out.println("enter the number to print fibo series :");
    int n=sc.nextInt();
    for(int i=0;i<n;i++){
    System.out.println(fibo(i));
    }

    }
    }