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

[4356 byte] By [RAWR-itsanONIONa] at [2007-11-27 5:10:42]
# 1
you need to learn to use methods.
TuringPesta at 2007-7-12 10:30:55 > top of Java-index,Java Essentials,Java Programming...
# 2

I think I understand the problem, but I'm unsure how to try to guide you.

What's happened to your indentation? I can't read your code, even with the code tags. You may also want to use more reader friendly variable names, like isFibonacci instead of d.

Your algorithm, as you describe it in plain English, sounds OK, go ahead with it.

I believe the previous reply contains more truth than you would think at first. Write a isFibonacciNumber() method, and you will have a chance to use it, first to decide whether the input number is a Fibonacci number, next, if it wasn't, after your subrtraction, to decide if the remaining number is (7-5=2).

Your isFibonacciNumber method would have to search through your array for the number. Read something about searching. You can use linear search (simpler) or binary search (faster).

If the user input was not a Fibonacci number, you want to find the closest down the list, again use either linear or binary search. Then use linear search backwards for a number where your subtraction trick works.

HTH

OleVVa at 2007-7-12 10:30:55 > top of Java-index,Java Essentials,Java Programming...
# 3
thanks alot guys, i'm going to try your suggestions and see if i can make any more progress!duke stars awarded for your helpfulness ^_^
RAWR-itsanONIONa at 2007-7-12 10:30:55 > top of Java-index,Java Essentials,Java Programming...