| | |
Please Help!!!
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2005
Posts: 2
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: May 2005
Posts: 55
Reputation:
Solved Threads: 1
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
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
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
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
For more help, www.NeedProgrammingHelp.com
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 < 10; i++) { System.out.println("Visiting row " + i); for(int j=0; j < 10; 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
Java Syntax (Toggle Plain Text)
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
Java Syntax (Toggle Plain Text)
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
Java Syntax (Toggle Plain Text)
//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;
![]() |
Other Threads in the Java Forum
- Previous Thread: Move out java code from jsp
- Next Thread: thx but how do i display an image in java
| Thread Tools | Search this Thread |
Tag cloud for Java
@param add android api apple applet application arguments array arrays automation bank binary binarytree bluetooth bold chat chatprogramusingobjects class classes client code compare component coordinates database digit draw eclipse editor error event exception fractal game givemetehcodez graphics gui guidancer health html ide image input int integer integration j2me jarfile java javac javame javaprojects jmf jni jpanel julia keytool learningresources linux list loop map method methods mobile netbeans newbie nonstatic number oracle pattern print problem program programming project projectideas recursion scanner screen server set sharepoint size sms socket sort sorting sql sqlserver string swing test text-file threads time tree web windows






