Do you know you can earn heaps of $$ by answering questions on kodlogs, for more info contact us through feedback link at footer? We are Australian based web site and pay you for answering questions

  • Register
Welcome to Kodlogs Q&A, where you can ask programming questions and receive answers from other members of the community. For programmers, by programmers Kodlogs is an open community for anyone that does coding. We help you get answers to your toughest coding questions, share knowledge with your coworkers in private, and find your next IT dream job.
0 votes
3 views
How do you find the square root in Java? How do you square a number in Java? Need

Java sqrt() with example?
by (8.8k points)  

1 Answer

0 votes
To find the square root of a number so for finding square root of a number what actually metals which we require is now I'm going to discuss first of all I will tell you a method to which we can use a library functions and to library function so you can find the square root of a number and attend with that what we will do is this by a simple method that is by incrementing each number by one time and then multiplying that number with that number only and taking out that this number comes out to be same as that number which are given for finding this packet and if it's come out to here same then sending that number in the a time and we will be using the third method that is the optimized method by taking a reference point I will tell you these three methods in detail first let's see by the wave functions suppose and if you ask you to write the square root of a number on you use that library function then obviously that is not the method with the interview looking so you come in to be in a big dilemma and that will be sad for you in that interview and you will cut a sorry figure in that interview so this is not recommended to use a very functions for the interview process and second method when you will use so let's see what you'll use in that method in that incrementing each number by one time then you can find the square root of number so the code which you write is take whether the number is zero or one if it comes out to be zero if it comes out to be 1 then obviously return that number only because the square root of zero and one of the same that is you and one only respectively well if the one comes out to be zero and one then you'll apply a while loop and in my looks will take a reference point that size and my father died with that I only to check whether it comes out to be the answer and if and the answer is same as a number which is required to be found this but that is the square root is required to found and if it comes out to be the same then simply return that I but the thing is that this is a general method which anyone can use and obviously the time complexity is not that good that is the square root of a number so obviously this is also not a good method to use for finding the square root and third method which I am going tell you is by taking the reference point by a reference point and telling you like suppose the number of emails and so what you'll do we basically start with an and one and end with and so the reference point which we take is the start for the start that is from one and four and you attack that number only that is the number which is given suppose 25 is given you will apply end as 25 and the reference point if you attacked me with an middle point that is the start point that's endpoint by 2 so in that process that is in this process of taking the reference point of start and and made by using this method you will come up for the time complexity of or divorce law and so this is a method which will be using fine this square root of a number so here I will be doing the podium dividing the three things which are told that is by library function and terrifying incrementing and harder by a reference of in Madrid and start point so what I've done here I've created a java function with our project and that the opposite I've made a class the default package only and in that class I require the main function and simply I am printing out subscriber table bases so they get square root function and Here I am l16 you can pass on I think that is when 515 9 anything so I'm passing in this function and I'm doing the code regarding this so let's do by first method which I was in a presentation that is by a wave function so I have a turn Matt's got shadowed of the number which I'm providing here but this is a good thing when you are coding and when you have actually adding the board but this is not recommended for any interview when you run this run as java application see what I found the square root is 4 so this by simply by using a very functions it but this is not a good thing to do in the interview process so I am commenting out this or simply I am deleting this and second thing which I will do is by incrementing that by simply from number 1 to the length so in that scene are you are right if the number becomes 0 or the number is 1 what I will do I will simply return the number because if number is 0 or F is number 1 then the square root of 0 & 1 is that same number that is 0 and 1 if s respectively only if this is not 0 and 1 then while or I will take to the different points that it is and I is equal to 1 comma answer is equal to 1 okay and what I will do if also is less than equal to the number then what you what to do is increment I by 1 each time increment I by 1 each time and answer will be I into I into I okay and I will check if this answer matches with the number then say Peyton I this return ok there is a second method so second with that and I will run this let me clear the control I have cleared the console run as java application ok see again we got the square root is 4 but this is by second method but this is not an optimized solution so the two instead of getting big o of square root of n we need Big O of log of and so in that when are you what I will do I will use third method by reference of eight in Maidan start so this I will keep it as it is that is number is equal to 0 1 and what I will do I will take some reference point that is start I will take in desert not only and I will take the answer 1 this is giving error because it's not an integer so I will do it is an integer and never return an integer only so I will take if start is less than equal to and and what I will do I will check three things that is if one more thing I'll take that is the mid and initialize mid is equal to start plus n by 2 32 the midpoint so I'll take a start and I will check if this mid into mid comes out to be the number then simply return made else if mid is less than the number that is end now simply say as number only median to mid then what to do simply increment the mid simply increment the start is equal to mid plus 1 and the sign and set has made only else what to do I'll make end to come at Alyssa point so if intimate comes out to greater than number number which we have to find the square root then what to do if it comes out to be greater than then making to be less than that end so it will become mid minus 1 so I'll in the end I will return answer and so okay so this will be end okay so I let you have the console and turn it once again then job application so again we got square root is 4 this is a third method that is optimizing the third we are taking the reference point midpoint start point and end point and we are thinking that MIT comes out to be that median dome it comes out to be the number 10 simply return the mate if it comes out to less than that number then directly we're incrementing mid we are making the start of mid plus one else we are decrementing the end and making it as made minus one so in this way we will do it in Big O of log off and time complexity .
by (8.8k points)  
...