Saturday 6 August 2016

Java Program to generate Triangle (Fibonacci)

Java Program to generate Triangle (Fibonacci)

Here you go with sample program!

public class Sample {

public static void main(String[] args) {
int a = 0, b = 1, i, c, n = 5, j;
for (i = 1; i <= n; i++) {
a = 0;
b = 1;
System.out.print(b);
for (j = 1; j < i; j++) {
c = a + b;
System.out.print(c);
a = b;
b = c;
}
System.out.println();
}
}
}


Output:
1
11
112
1123
11235

enjoy......................

1 comment:

  1. I tried to type these codes for two times and, it didn't work at all. Also, I didn't find any solution in Internet. Can anybody, who knows, explain me?

    ReplyDelete