Tuesday, August 18, 2009

Fibonacci Series

class FibonacciNumbers
{
public static void main(String[] args)
{
System.out.println("Hello World!");
if(ContainNumber.onlyNumbers(args[0])){
int number = Integer.parseInt(args[0]);
int num1;
int num2= 0;
int num3 = 1;
while(number>0){
System.out.println(" F Series : " + num2);
num1 = num2;
num2= num3;
num3 = num1+num2;
number--;
}

}


}
}

4 comments:

  1. Good program... i will give you 8.5/10 ... there are some scope of improvement...

    I have added an extra print statement in the loop to print the value of the loop variables viz. number, num1, num2, and num3.
    Now, i want you to explain me the every observation you can make.
    I believe that programming is not just writing a piece of code, but also understand that what is happening due to the program.

    Output:
    *****************
    C:\Java>java FibonacciNumbers 4
    Hello World!
    In side only Number method
    Enteres Number is valid Number
    F Series : 0
    >> INFO #4: 0 : 1 : 1
    F Series : 1
    >> INFO #3: 1 : 1 : 2
    F Series : 1
    >> INFO #2: 1 : 2 : 3
    F Series : 2
    >> INFO #1: 2 : 3 : 5

    C:\Java>
    *****************


    Modified Code:
    *****************
    class FibonacciNumbers
    {
    public static void main(String[] args)
    {
    System.out.println("Hello World!");
    if(ContainNumber.onlyNumbers(args[0])){
    int number = Integer.parseInt(args[0]);
    int num1;
    int num2= 0;
    int num3 = 1;
    while(number>0){
    System.out.println(" F Series : " + num2);
    num1 = num2;
    num2= num3;
    num3 = num1+num2;
    System.out.println(" >> INFO #" + number + ": " + num1 + " : " + num2 + " : " + num3);
    number--;
    }
    }
    }
    }
    *****************

    ReplyDelete
  2. //litte change in my programe home u will like it .

    class FibonacciNumbers
    {
    public static void main(String[] args)
    {
    int number = Integer.parseInt(args[0]); // take the number from argument
    String fsereis = "";
    if(ContainNumber.onlyNumbers(args[0])){
    int num1;
    int num2= 0;
    int num3 = 1;
    while(number>0){
    num1 = num2;
    num2= num3;
    num3 = num1+num2;
    fsereis = fsereis + " " + num1;

    number--;// decrement the number by one
    }
    System.out.println(" >> POSTIVE Fibonacci Numbers Sereies for numbber "+ Integer.parseInt(args[0]) + " is :" + fsereis);
    }
    }
    }

    Out put is : >> POSTIVE Fibonacci Numbers Sereies for numbber 7 is : 0 1 1 2 3 5 8

    ReplyDelete
  3. this change is good, but the question i asked in the previous comment is still unanswered ...
    Q: I have added an extra print statement in the loop to print the value of the loop variables viz. number, num1, num2, and num3.
    Now, i want you to explain me the every observation you can make.
    See the modified code and output in the previous comment.

    ReplyDelete
  4. from the F Series : sout looks like we are going to print F Series : for 0,1,2,3
    from the INFO #" : sout look like we rae printin Fab sereris for 5 which is 0,1,1 for 4 :1 : 1 : 2,for 3 1 : 1 : 2,for 2: 2 : 3 : 5,for 1: 3 : 5 : 8

    ReplyDelete