0% found this document useful (0 votes)
37 views6 pages

Worksheet 2

The document describes an experiment to write a Java program that prints the nth value of the Fibonacci sequence using both recursive and non-recursive functions. It provides an overview, algorithm, code sample, and output of a program that takes user input for n and prints the nth Fibonacci number using a non-recursive for loop approach.

Uploaded by

Raghav Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views6 pages

Worksheet 2

The document describes an experiment to write a Java program that prints the nth value of the Fibonacci sequence using both recursive and non-recursive functions. It provides an overview, algorithm, code sample, and output of a program that takes user input for n and prints the nth Fibonacci number using a non-recursive for loop approach.

Uploaded by

Raghav Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment No.1.

2
The Fibonacci sequence is defined by the following rule. The first 2 values in the sequence are 1, 1. Every subsequent value is the
sum of the 2 values preceding it. Write a Java program that uses both recursive and non-recursive functions to print the nth value
of the Fibonacci sequence.

Student Name: Ashwin Tomar UID: 20BCS6180


Branch: CSE AIML Section/Group: AIML-1 B
Semester: 3 Date of Performance:31-aug-2021
Subject Name: PROGRAMMING IN JAVA LAB
Subject Code: 20CSP-235

1. Aim/Overview of the practical:

Write a program to Print nth value of Fibonacci Series using recursive and non-recursive
function.

2. Task to be done:

Write a program to Print nth value of Fibonacci Series using recursive and non-recursive
function.
3. Algorithm/Flowchart:

1. Create a scanner class object


1. Take Range Input From user
2. For Non-Recursive function use for loop and inside for loop swap and sum the values
3. For Recursive function use logic (range-1) + (range-2) with return statement
4. Print the result

4. Steps for experiment/practical:


1. Take Input from User
2. if range is less than equal to 1 print value
3. Find nth value with the help of Above-Algorithm
4. Print the Result
5. Code:

import java.util.Scanner;
class Main{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int n1=0 ,n2=1 ,n3 ,i ;
System.out.print("Please enter number of output you need : ");
int count = input.nextInt();
System.out.print(n1+" "+n2);
for(i=0;i<count-2;++i)
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;

}}
6. Result/Output:
Learning outcomes (What I have learnt):

 Use of Scanner Class


 Java Syntax Use of Conditional Statement
 Java Input and Output Functions
 Arithmetic operation and Math Class

Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):

Sr. No. Parameters Marks Obtained Maximum Marks


1.
2.
3.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy