954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Simple Fibonacci Program - What am I Doing Wrong?

I am doing a simple program to calculate the first 15 fibonacci numbers and also placing an asterisk next to even numbers. My output should look like this:
1
1
2*
3
5
8*
13
21*
34*
55
89
144*
233
377
610*

I can calculate the numbers easily enough but I can't seem to get the asterisks next to the even numbers. My code so far is:

package assign4;

/**
*
*
*/
public class Main {

/** Creates a new instance of Main */
public Main() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) { //a method
int num1 = 1; //first number in fibonacci sequence
int num2 = 1; //second number in fibonacci sequence
int num3 = 2; //provides summation of previous two numbers



System.out.println("The first 15 fibonacci numbers are: ");
System.out.println(num1);
System.out.println(num2);


while( num3 <= 377){ //loop ends after calculating the first 15 fibonacci numbers
num3 = num1 + num2;
System.out.println(num3);
num1=num2;
num2=num3;

}

if ( num3 % 2 == 0 );
System.out.println(num3 + "*");
num1 = num2;
num2 = num3;

It is a very basic program and I'm sure I look like a fool by overlooking an obvious flaw, but I'm only in my first year of Computer Science so I'm still new to this. If anyone could lead me in the right direction it would be greatly appreciated.

ColicabSyke
Newbie Poster
2 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

It's simple .
Before displaying that number check whether it an even number
while( num3 <= 377){ //loop ends after calculating the first 15 fibonacci numbers
num3 = num1 + num2;
if ( num3 % 2 == 0 )
System.out.println(num3 + "*");
num1=num2;
num2=num3;
}

not after displaying
No need for this code fragment
/*if ( num3 % 2 == 0 );
System.out.println(num3 + "*");
num1 = num2;
num2 = num3; */

greetings
Parthiban

parthiban
Junior Poster in Training
80 posts since Sep 2006
Reputation Points: 10
Solved Threads: 6
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You