Program help please!
okay, so first of all i want to thank the people that helped me to figure out what was wrong with where i put my loops and statements in my fibonacci program but now there is another problem (ack!).
If the user's input is not a fibonacci number the program should output that it is not and output what fibonacci numbers add together to create that user's input.
Example, if the number inputted is 7, the program would output 5 + 2
I'm not sure at all how to do this and i've been trying for a while. I'm thinking that the program should check the fibonacci number closest down (as in the one lesser than it) and then minus that from the number to create a remaining number (in this case 7-5 = 2) and then do the same thing, if the number left over is a fibonacci number then output that. However, i'm not sure how to do that either, to check for the closest number and stuff.
Well, Here is my code so far:
import java.io.*;
import java.util.*;
publicclass TrivialApplication{
publicstaticvoid main(String args[])throws IOException{
System.out.print("If you want to exit this program");
System.out.println(" just enter 0.");
BufferedReader kinput =
new BufferedReader (new InputStreamReader (System.in));
int user_input = 1;
int c = 0;
boolean d =false;
while (user_input != 0){
System.out.print("\nPlease enter a positive integer: ");
try{
user_input = Integer.parseInt(kinput.readLine());
}catch (NumberFormatException nfe){
System.out.print("\nI said a positive integer: ");
user_input = Integer.parseInt(kinput.readLine());
}
int[] int_array =newint[40];
int_array[0] = 1;
int_array[1] = 1;
for( c = 2; c < int_array.length; c++){
int_array[c] = int_array[c - 1] + int_array[c - 2];
if (user_input == int_array[c]){
d =true;
}
}
if (user_input == 1){
d =true;
}
if (user_input == 0){
System.out.println("Program has ended.");
}
if (d ==false){
System.out.println("That's not a fibonacci number!");
}
if ((d ==true) && (user_input != 0)){
System.out.println("That's a fibonacci number!");
d =false;
}
}
}
}
Thank you all for your help, this is a toughie i can't understand -.-
Message was edited by: RAWRitsanONION
Reason: code tags!
RAWR-itsanONION

