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.

Dani AI

Generated

Useful clarification and a couple of practical tips to fill gaps in the replies from , and .

Problem 1 (table): treat the table as a 10x10 grid. Using zero-based row r and column c the cell value follows a simple formula: value = 10 * (r*10 + c + 1). Computing each cell from that formula avoids error-prone chained increments. Print each row on one line and use fixed-width formatting (for example with System.out.printf("%4d", value)) so columns line up; print a newline after each row.

Problem 2 (square root): the iterative method already posted converges well, but the implementation details matter. Use double for all math (avoid integer division), handle n == 0 as a special case, and reject or document negative inputs (the real square root is undefined for negatives). Stop iterating using a robust criterion such as the absolute residual |g*g - n| < eps or a small relative tolerance; also cap iterations (e.g., 50–100) to prevent infinite loops. Comparing the result against Math.sqrt(n) during testing is a quick correctness check.

Common pitfalls and troubleshooting: accidentally using int math, missing the newline between rows so output runs together, infinite loops from too-strict stopping logic, and division-by-zero if a guess becomes zero. As suggested, tracking the previous guess works, but the squared-residual test is usually clearer. Use small test values (perfect squares, tiny numbers, large numbers) to validate correctness and precision.

Recommended Answers

All 3 Replies

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,

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,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.