hi guys.
i have written a prog. wich prints out the the first 5o numbers. but unfortunatly i get negative and diffrent number in the last 3 number instead of getting postive number.
please help otherwise i will fail the whole course(
i got
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155 165580141 267914296 433494437 701408733 1134903170 1836311903 -1323752223 512559680 -811192543
but i have to get 2971215073, 4807526976 and 7778742049
PHP Code:
[LEFT]public class Fibona {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in); //create new scanner
//Prompt user for length
System.out.println("How many fib terms would you like to display? ");
int lenght = scan.nextInt();
int[] p = new int[lenght]; //set new array p equal the defined user length
printTerms(p, lenght); //method to print terms
}
//main end
//method to populate and calculate array and fib sequence
public static int printTerms( int[] p, int lenght ) {
//print first two terms
p[1] = 0;
p[2] = 1;
int newTerms = 0; //new value to calculate new terms on the sequence other that the first two
if ( lenght == 1 )
System.out.println(p[1]);
else if ( lenght == 2 )
System.out.println( p[1] + " " + p[2]);
else { //print rest of terms
System.out.print( p[1] + " " + p[2] + " ");
//for loop to calculate the rest of the terms
for ( int index = 3; index <= lenght; index++) {
newTerms = p[1] + p[2];
System.out.print( newTerms + " ");
p[1] = p[2];
p[2] = newTerms;
}
}
return newTerms;
}
}[/LEFT]