• Register
Welcome to Kodlogs Q&A, programming questions and answer website.

Most popular tags

java program python php javascript android c# r spring c mysql eclipse arrays python-3 exception tomcat ggplot2 spring-boot numpy html sql-server jdbc ssl sockets windows string x python-2 hibernate 7 java-8 intellij-idea ssl-certificate entity-framework macos pandas minecraft ios csv security sql json asp list net class heap-memory for-loop scikit-learn winforms illegalstateexception keytool matlab redirect code program junit pip maven ubuntu loops math facebook datetime jquery cryptography loop int import https tsql fatal-error struts java-applet spring-mvc runtime-error apache-spark pyspark garbage-collection amazon-web-services sql-server-2005 quirks-mode internet-explorer-9 internet-explorer reporting-services anova machine-learning keras indentation-error unit-testing android-studio jupyter-notebook glmnet virtualenv webclient visual-studio-2010 webpack compiler-errors numpy-ndarray curl vagrant aspnet reporting dataframe nullpointerexception javafx tensorflow jsp django jpa docker wordpress servlet indentation xcode typeerror scanner sum deployment socketexception git linux certificate css util oracle cmd node recursion search tcp syntax connection float httpwebrequest js main function number file html5 spring-data-jpa softmax elastic-beanstalk google-sheets-api google-spreadsheet-api cnf minidump xuggler acumatica gitlab elasticsearch-5 elasticsearch app-transport-security ios10 ios9 cdn cloudflare wampserver plpgsql salt-stack sysv parent-child android-edittext textview visual-studio-2012 android-workmanager unique-index unity2d unity3d electron theano maven-compiler-plugin pickle assetbundle rstudio bar-chart python-tesseract tomcat6 primefaces solr easymock jvm-crash tomcat7 apache-httpclient-4 web-services woodstox discord jersey-2 prepared-statement resultset sencha-cmd sencha-touch-2 sencha-touch powermock fips apex x509certificate ibm-bpm websphere-7 file-permissions file-io accesscontrolexception grizzly atmosphere slick2d lwjgl informetica twitter-bootstrap-3 jax-rs resteasy spring-security-kerberos spring-security kerberos long-integer mapnik pytorch git-review vpython continuation homebrew xgboost conda visual-studio android-asynctask stack-trace user-interface jaxws-maven-plugin maven-3 maven-2 browser-notsupportedexception google-maps-api-3 google-maps visual-studio-code truststore firebase-authentication aws-organizations php-7 string-formatting cplusplus visual-studio-2015 net-core net-mvc msbuild extension-methods foreign-keys windows-services react-redux reactjs inputstream facebook-graph-api entity-framework-4 reportingservices-2005 mips linear-regression deep-learning block-device melt reshape2 floating-point webpack-dev-server javascrip 0-lollipop android-5 statsmodels avx eclipseide javafx-2 php-not-recognized laravel command-line tinyurl classnotfoundexception atom-editor android-emulator android-sdk-tools ionic2 cordova foobar2k tcplistener net-2 net-4 ole-db-provider export-to-excel openrowset windows-10 vagrantfile jvm-arguments global-variables orm autopostback classformatexception eof upstart qt ipv4 graphviz pydot atom curses unityeditor pandoc tesseract xls oledb redux spotify cxf wamp nio stacked prediction headless hyperlink outlook jackson keystore applicationcontext pipeline iis jstl encryption perfect-square objective-c carthage xcode8 indexing standard-deviation sudo xampp postgresql connector apple mdf kubernetes destructor gettime arithmeticexception gmail ioexception heuristics milliseconds disk cpu npm modx-revolution goldsky modx prevnext javascript-dictionary stack-smashing device-monitor radio-button android-actionbaractivity android-activity android-fragments java-long unqualified-id ora-12154 javc c++ java nullpointerexception runtime-error drjava awt-eventqueue dsx math-pow ajquery nosuchelementexception appcompatactivity jsf jtextfield awt jpanel inputmismatchexception glassfish deque jupyter flask lvalue gradle servlets netbeans simulation ibm factorial javax apache boot opengl virtualbox jvm margins 2147483647 mongodb cloud firebase plot plugins github processor automation crash card pointers editor concatenation formatting debugging devices module testing color arraylist sequence nested date caching expected build response ip directory algorithms release collections print figure casting url expression validation integer microsoft sorting sort read types replace rest ajax exe excel dictionary message required database text variable time size dll system files table runtime static random code web map http error version 0 2 my
0 votes
4 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.9k 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.9k points)  
...