Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,640 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,075 people online right now. Registration is fast and FREE... Join Now!




square root

 
Reply to this topicStart new topic

square root

sibongile
post 12 Oct, 2008 - 03:00 AM
Post #1


New D.I.C Head

*
Joined: 12 Oct, 2008
Posts: 1

a program that calculates the square root of a number accepted by the user through the keyboard
User is offlineProfile CardPM

Go to the top of the page

William_Wilson
post 12 Oct, 2008 - 03:49 AM
Post #2


lost in compilation

Group Icon
Joined: 23 Dec, 2005
Posts: 3,970



Thanked 15 times

Dream Kudos: 3275

Expert In: Java, C, Javascript

My Contributions


take a look through the Math library.

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.
User is offlineProfile CardPM

Go to the top of the page

Gloin
post 12 Oct, 2008 - 05:11 AM
Post #3


On MeD.i.Cation

Group Icon
Joined: 4 Aug, 2008
Posts: 717



Thanked 46 times
My Contributions


If you're not allowed to use the Math class then what you do is try some divide and conquer approach.

If the input number (number) is greater than 1:

Set anum = number
Set bnum = 1
(Loop)
Set tnum = (anum + bnum) / 2
Calculate the square of tnum. (cnum = tnum * tnum)
compare cnum to number
if equal (within some reasonable interval cnum = number +/- epsilon where epsilon is a very small constant) print cnum and Exit the loop (best implemented with a while-statement.
else if (cnum < number), set bnum = tnum
else if (cnum > number), set anum = tnum
(end Loop)


The case where number is less than 1 is very similar and you will have to consider some special cases where number = 0 or 1. This algorithm is very simple to translate into code.

This post has been edited by Gloin: 12 Oct, 2008 - 05:13 AM
User is offlineProfile CardPM

Go to the top of the page

BigMike7319
post 12 Oct, 2008 - 08:27 AM
Post #4


New D.I.C Head

*
Joined: 12 Oct, 2008
Posts: 14

I am a beginner with Java myself, and I recently had a program like this

I think what you are asking is how to possibly get the input from the user, and have it output the answer to the screen.

What I do to help myself with some of the programs is to make some pusedo code of what exactly needs to happen,
or make a flowchart so you can physically see what you want the program to do.

Here are some tips that helped me, mabey they will work for you as welll;

*I do all my initial stuff on paper first, its just helps me before I start the actual code

*Ask yourself what input you want from the user
then capture it

*Ask yourslef what output you want to print to the screen
then capture it

*Try to declair your variables first, then after they are declaired skip down a few lines
and initialize them

*Start off with your first promt to the user

*Then do the second promt (if any)

*Code your algorythm then output it to the screen

*Alawys show the line numbers, this really helps when you compile it
it will tell you the line the errors are on, if any, and give you a suggestion
as how to fix it.

Hope this helps you a bit and dosent confuse you, post your code to the forums when you get it if you
have any more questions.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 04:30AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month