Hello everybody, actually i'm having problems with java loops as it is so complicated, dont know where to start. I have 2 questions if u could please answer it.

1. Write a program that generates and ouputs the table below:

10 20 30 .... 100
110 120 130 ....200
210 220 230... 300
. . . .
910 920 930... 1000

2. The square root of a number is computed as follows:
guess = n/2.0

repeat
guess = (guess+ n/guess)/2.0
until guess comes to very close to answer.

Write a program to find the sqaure root of a number a positive.

You have several different types of loops


while (condition is true)
{}

for(variable declaration;while condition is true;variable increment/decrement)
{
}

for(int i=0; i<something; i++)

Complicated? If you think a basic loop is complicated you'd better give up now and look for employment as a bellboy in a hotel without an elevator.

For the first problem, you must use two for loops. Lets call the horizontal direction i and the vertical direction j.

Notice how the #'s change 10 times from 10 to 100 in direction i
Notice how the #'s change 10 times from 10 to 210 in direction j

Therefore use a loop that looks like

for(int i =0; i < [B]10[/B]; i++)
{
	System.out.println("Visiting row " + i);

	for(int j=0; j < [B]10[/B]; j++)
	{
		System.out.println("\tVisiting col " + j + " in row " + i);				
	}
}

The outer loop runs once for each row (there are 10 rows).
For each row, the inner loop runs once for each col (there are 10 columns) in that row.
Therefore, it will "visit" the table from top to bottom and left to right.
It prints the following

Visiting row 0
	Visiting col 0 in row 0
	Visiting col 1 in row 0
	Visiting col 2 in row 0
	Visiting col 3 in row 0
	Visiting col 4 in row 0
	.
	.
	.
Visiting row 1
	Visiting col 0 in row 1
	Visiting col 1 in row 1
	Visiting col 2 in row 1
	Visiting col 3 in row 1
	Visiting col 4 in row 1
	.
	.
	.
.
..
etc

How do the number's change in the i direction?
They always increment by 10.

How do the number's change in the j direction?
They always increment by 100.


You must create some variables before the two loops called currentValueInIDirection and currentValueInJDirection. The variables will change value as you generate the table.
These variables you must increment according to the rules above.

For more help, www.NeedProgrammingHelp.com

For the second problem, ask the user for input by writing

System.out.println("Enter a guess");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());

Use the while loop and the formula given.

The hardest part is to know when to stop. The variable guess is constantly being updated in the loop. When its changes by a small amount (say .01) then it is safe to stop the loop.
To do this, create a separate variable called guessPrevious. Then do the following in the loop

//save the guess before changing it
guessPrev = guess;

//update guess using formula
guess = (guess+ n/guess)/2.0

//stop loop if small change
if( Math.abs(guessPrev-guess) < .01)
	break;

For more help, www.NeedProgrammingHelp.com

This article has been dead for over six months. Start a new discussion instead.