 An official Batch Programming blog! To Solve the daily problems with windows and fixing them with little notepad scripts. Unlock the Hidden Secrets of Windows, Learn to crack Software via CMD & Make your own Programming tools. Understand to Manipulate your PC via CMD.

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.

Moreover, if you want to make such a batch program that can calculate sqrt for you, then you need to have a different point of approach. Because batch doesn’t allow you to simply calculate the Floating-Point Values!

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.

## LOGIC

If a number is perfect square (e.g. 4,16,25 …) then if we subtract odd numbers series elements (e.g. 1,3,5,7 …) then at last we get perfect ‘0’ otherwise, the number is not a perfect square and it needs to be calculated only by long division.

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 BATCH?

If you read the code carefully, you’ll know how the program works and you even can’t recognize that I am using VBS for calculating sqrt of non-prefect squares.

``````@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``````