NPTEL » Programming In Java Week 3 : Programming Assignment 1 Aug 2022

 

Week 3 : Programming Assignment 1

Due on 2022-08-18, 23:59 IST

This program is related to the generation of Fibonacci numbers.

For example: 0,1, 1,2, 3,5, 8, 13,… is a Fibonacci sequence where 13 is the 8th Fibonacci number.

A partial code is given and you have to complete the code as per the instruction given below.

import java.util.Scanner;

public class Fibonacci {


public static void main(String args[]){

Scanner sc = new Scanner(System.in);

int n=sc.nextInt();

    System.out.println(fib(n));

}

//Template code:

static int fib(int n) {

    if (n==1)      //Terminal condition

    return 0;

    else if(n==2)

    return 1;

    return fib(n - 1) + fib(n - 2); //Recursive call of function

    }

}







No comments

JavaFX Scene Builder

  This is an article about the JavaFX Scene Builder. You will get a short introduction about the installation and usage of the software. The...