PDA

View Full Version : URGENT HELP NEEDED WITH Iterative statements – for loops



mayjarboi
March 31st, 2008, 08:20 PM
i need a program which outputs a series of asterisks (* - stars) to form the following pattern below.

i think in order to get the second half of the pattern you will need another set of for loops, this time counting down or you can try to change over the loop control and decrement after 5.

The output should look like: (this is a short version, you should have 10 lines of stars)



*
**
***
****
*****
******
*****
****
***
**
*


i then need a METHOD which produces the Fibonacci series when called.

The series is formed by adding two numbers in the series to generate the third. Note the first two are 0 and 1, so to make the make number we add 0 + 1 to give 1. The fourth number is obtained by adding the second and third numbers, 1 + 1 gives 2. So the series looks like:



0 1 1 2 3 5 8 13 21………

All help is really appreciated and i may be able to reward any one for their help. Thanks

Charleh
April 1st, 2008, 06:50 AM
Which language would you like this program in?

Is this for school/college/uni whatever?

mayjarboi
April 1st, 2008, 11:40 AM
sorry i wanted it in java please

bluefootedpig
April 1st, 2008, 03:03 PM
doesn't really matter what language it is for, and i'm going to guess it is in college seeing how this is like an introduction type problem.

yes, you want two loops for the * problem. One to count up, one to count down.

count = 5

for (int x = 0; x < count ; x++)
{
WriteStars(x);
}


for (int x = count; x > 0 ; x--)
{
WriteStars(x);
}


public void WriteStars(int count)
{
for (int x=0; x< count; x++)
Write('*');
Write('\n');
}


thats in c# or c in general, i'm sure you can figure out hwo to change it over to java.

for the next problem (i so bored i'm actually answering this)...

public void Fib(int prev, current)
{
int next = prev + current;
write (next);
fib(current, next);
}

now you didn't say how to really start it off, form main you could call Fib(1,1) or you can make a function called StartFib which just does the Fib(1,1). Also you didn't say how many times to do it, so this will go on forever. If you want it to stop, i would put some sort of break condition like eithre a number cap or a loop cap.