In this Post, I Am gonna teach you ‘How to calculate sqrt of a number in Batch?‘ Calculating Square root (sqrt) of a number is a difficult and very time-consuming Task.
If you are using the traditional ‘Long division method’ or Any other Square root formula, if you have very less practice of the method, then it may become Pain in the butt.
In this post, I’m gonna tell you how to make a simple Batch Program that can calculate the sqrt of a number (that you’ll enter in it).
The different approach here is, in the method of calculation as i’m not telling the computer how to use long division method, rather I’m using an alternate method.
If a number is
I’m using a loop to calculate sqrt of perfect squares, and I’m using VBS (instead of defining long division) to fulfill my requirement.
HOW TO CALCULATE SQRT OF A NUMBER IN
If you read the code carefully, you’ll know how the program works and you even can’t recognize
@echo off :top cls Title Sqrt Calculator... by Kvc echo. echo. set /p "no=Enter The Number to calculate Sqrt : " if not defined no goto top set /a "no=%no%" set sub=1 set sqrt=0 set bkup=%no% Rem Calculating Sqrt here.... Rem Using Property of Perfect sqr, that we have learned in small Classes...that if we subtract odd nos. from perfect sqr,then rem atlast perfect '0' will be left otherwise not.... :] :loop if %no% gtr 0 ( set /a no=%no%- %sub% set /a sub+=2 set /a sqrt+=1 goto loop ) goto end :end REM Checking if the answer is correct..as cmd doesn't support float values.... :[ set /a sqr=%sqrt%*%sqrt% if "%sqr%" == "%bkup%" ( Echo. Sqrt : %sqrt% ) ELSE ( echo. wscript.echo %bkup%^^(1/2^) >"%tmp%\a.vbs" for /f "skip=3 tokens=*" %%a in ('cscript "%tmp%\a.vbs"') do echo. %%a ) echo. pause Del /q "%tmp%\a.vbs" goto top rem #kvc