# How to print Fibonacci Series in Java without using For Loop?

Hi All,

I have tried to print with for loop - here is the code look like

``````import java.util.*;
public class fibonacci{
public static void main(String args[]){
int n,i;
Scanner sc= new Scanner(System.in);
n=sc.nextInt();
sc.close();
int values[]=new int[n]; // space O(N) used
values=0;
values=1;
for(i=2;i<n;i++)values[i]=values[i-1]+values[i-2]; // single traversal O(N)
System.out.println("The nth fibonacci number is "+values[n-1]);
}
}
``````

But I don’t know if is there any way to print without for loop.

Any help in this?

1 Like

Love this interview question.
There are two ways - the loop and the recursion. Here’s the second approach - Program for Fibonacci numbers - GeeksforGeeks

3 Likes

I was referring to these articles for knowledge:

What do you suggest me to stick on for resources?

Resource 1 breaks it down easiest, but you have the option of going with a While Loop or using pure Recursion.

What are your actual requirements to complete this?

I see this as three levels.

1. Probably convert to recursion

2. Use Dynamic programming with a hashmap

3. For modern enterprise I would say Java has streams now so create an infinite stream and then foldleft on it and then something to take n number of values from the stream to print

2 Likes

If you wanted to go dumb, you could maybe use inheritance and a factory. Just an idea though 2 Likes

How would this look like in Java.

I (haven’t worked in Java since 2010, … thankfully, a fair bit rusty), but I can imagine e.g. a c++ templates implementation.

That’s definitely the Enterprise™ way to do it.

4 Likes

This is the obfuscated way to do it … still has a print though…

``````perl -e 'print\$f+=\$z=\$f-\$z,\$/for--\$z..100'
``````
1 Like

I guess you can still make a decent living as a perl programmer where it’s needed. Not sure that one-liner would help you get a job somewhere else though. 4 Likes

Unfortunately, that was not mine … I don’t know perl innards well enough to come up with that, but perl surely lends itself to the opposite of Java’s practices of creating factories and interfaces for everything Sure is. Just write a lot of prints…

3 Likes

IIRC, you would automatically generate classes that each inherit some value from their respective superclass. The last generated class would then contain the fibonacci number from position x, when they all have a print statement in them, the sequence comes automatically.

In JVM but Scala I bet there is a way to define it in Scala 2/Shapeless or Scala 3. I know you can define natural numbers as compile time types.

``````  trait Nat
class _0 extends Nat
class Succ[A <: Nat] extends Nat
``````
1 Like

@MazeFrame I have the ultimate enterprise solution. Just deploy mechanical turk and make them calculate it by hand.

https://www.mturk.com/

3 Likes

Or, the Real enterprise way, import an existing , abandoned library that does it?

And then bricks all of dev that gets built relying on an abandoned project when it gets shuttered in a huff?

Internet stolen example

Fibonacci Series in Java - Javatpoint

``````
class FibonacciExample2{
static int n1=0,n2=1,n3=0;
static void printFibonacci(int count){
if(count>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
System.out.print(" "+n3);
printFibonacci(count-1);
}
}
public static void main(String args[]){
int count=10;
System.out.print(n1+" "+n2);//printing 0 and 1
printFibonacci(count-2);//n-2 because 2 numbers are already printed
}
}

``````
2 Likes

Hey all thanks for all your information this ideas really help me a lot.

1 Like

Hello,
You can print Fibonacci by using this following code

class FibonacciExample1{
public static void main(String args[])
{
int n1=0,n2=1,n3,i,count=10;
System.out.print(n1+" "+n2);//printing 0 and 1

for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}

}}

Since I didn’t see a real Java solution yet without a for loop, I decided I’d go for it here:

``````public class Main {
public static void main(String[] args) {
int limit = 10;

printFibonacci(limit);
}

private static void printFibonacci(int limit) {
Stream.iterate(Map.entry(0, 1), entry -> Map.entry(entry.getValue(), entry.getKey() + entry.getValue()))
.limit(limit)
.map(Map.Entry::getKey)
.forEach(System.out::println);
}
}
``````

This works because Stream.iterate produces an infinite stream of values, starting with the initial pair of (0, 1) and repeatedly applying that lambda on the previous value. The stream then limits the number of iterations and maps to the first value only in order to print it out. This program produces the following value for the limit of 10:

``````0
1
1
2
3
5
8
13
21
34
``````

What famous nerd man is telling you to do is to USE recursion. Your methodology for printing is up to you.

``````public class main {

static int r1=0, r2=1, r3=0;

static void fiboPrint(int count){
if(count>0){
r3 = r1 + r2;
r1 = r2;
r2 = r3;

System.out.print(" "+r3);

fiboPrint(count-1);

}

}

public static void main(String args[]){

int count=10;
System.out.print(r1+" "+r2);

fiboPrint(count-2);

}

}
``````

This is a set of resources on recursion

Remember something fundamental about it. Recursion almost always uses more memory and is fairly slow computationally because of its order.

2 Likes