Program To Print a Numbers from Fibonacci Series



class FibonacciSeries // By Steve
{
public static void main (int a)
{
int x=0,y=1;
int d=0;
System.out.print(x+",");
System.out.print(y+",");
for(int i=1;i<=a;i++)
{


d=x+y;
System.out.print(d+",");
x=y;
y=d;
}
}
}



Input: 14


Output:




14 comments:

  1. Can you explain me this:
    The variable "i" is not declared in the program...but still,
    there is this statement- "for(int i=1;i<=a;i++)"
    How does this program work?

    ReplyDelete
    Replies
    1. 'i' is declared itself in the for statement
      i.e for(*int* i=1;i<=a;i++)

      Delete
    2. i is in the parameter part....

      Delete
    3. int i is declared in the for loop.
      the 'int' keyword does the trick.

      Delete
    4. int i is declared in the for loop.
      the 'int' keyword does the trick.

      Delete
    5. It is not declared in Program...But is declared in for loop statement

      Delete
  2. 'i' is declared itself in the for statement
    i.e for(*int* i=1;i<=a;i++)

    ReplyDelete
  3. 47 doesn't work, how do you fix overflow

    ReplyDelete
    Replies
    1. Use try{} block to fix the overflow exceptions

      Delete