F5F Stay Refreshed Software General Software How to create a program that displays the Fibonacci sequence up to n?

How to create a program that displays the Fibonacci sequence up to n?

How to create a program that displays the Fibonacci sequence up to n?

F
Frankette44
Posting Freak
809
12-30-2025, 10:55 AM
#1
Here is a revised version of your text with the same structure and length preserved:

Java:
import java.io.*;

import java.util.*;

public class Main

{

//Recursive method to generate the Fibonacci sequence

static int fib(int n)

{

int res;

if(n <= 1)

return n;

else

{

res=fib(n - 1) + fib(n - 2);

}

return res;

}

public static void main(String[] args)

{

System.out.println("\nFIBONACCI SERIES USING RECURSION\n--------------------------------\n");

Scanner sc = new Scanner(System.in);

int n,i = 0;

System.out.println("Enter the number:");

n= sc.nextInt();

System.out.println("\nOutput\n-------”)

while(i++ < n)

{

System.out.println(fib(i) + "");

}

}
F
Frankette44
12-30-2025, 10:55 AM #1

Here is a revised version of your text with the same structure and length preserved:

Java:
import java.io.*;

import java.util.*;

public class Main

{

//Recursive method to generate the Fibonacci sequence

static int fib(int n)

{

int res;

if(n <= 1)

return n;

else

{

res=fib(n - 1) + fib(n - 2);

}

return res;

}

public static void main(String[] args)

{

System.out.println("\nFIBONACCI SERIES USING RECURSION\n--------------------------------\n");

Scanner sc = new Scanner(System.in);

int n,i = 0;

System.out.println("Enter the number:");

n= sc.nextInt();

System.out.println("\nOutput\n-------”)

while(i++ < n)

{

System.out.println(fib(i) + "");

}

}

W
Wolphinferno
Junior Member
17
12-30-2025, 10:55 AM
#2
try running it?
W
Wolphinferno
12-30-2025, 10:55 AM #2

try running it?

H
145
12-30-2025, 10:55 AM
#3
This appears to be a typical homework assignment, or perhaps a common question in a job interview. You need to consider the process of creating a Fibonacci sequence, then implement it in code. And you must be able to execute it to confirm the outcome.
H
hamsterdance00
12-30-2025, 10:55 AM #3

This appears to be a typical homework assignment, or perhaps a common question in a job interview. You need to consider the process of creating a Fibonacci sequence, then implement it in code. And you must be able to execute it to confirm the outcome.

L
LCAlex33c
Junior Member
35
12-30-2025, 10:55 AM
#4
Following the earlier updates, your code seems to be flawed. I’m unsure about its functionality without further details. Let’s review the output and compare it to the Fibonacci sequence to see where discrepancies arise. Understanding each step manually will help clarify the process and improve your coding clarity.
L
LCAlex33c
12-30-2025, 10:55 AM #4

Following the earlier updates, your code seems to be flawed. I’m unsure about its functionality without further details. Let’s review the output and compare it to the Fibonacci sequence to see where discrepancies arise. Understanding each step manually will help clarify the process and improve your coding clarity.