Introduction: How to Create Calculator in Batch

Before creating a calculator lets go through a simple code tutorial..
<====================================================>
1>(echo) code "echo" are used to write a text for e.g " echo Welcome to Instructables".
2>(color 30)code "color" are used to give color to background and font.
3>(set /p =)code "set /p" is used to create console in program.
4>(title)code "title" is used to give title to program
5>(start)code "start" is used to start an external program.
6>(pause)code "pause" is used to pause a program to avoid, crash.
7>(%username%)code "%username%" just adds the name of the profile name of respective OS.
<=====================================================>
"WIP" (Work-in-progress).

@echo off
title calculator
color 0A
:main
echo.
echo ---------------------------
echo.
echo WELCOME %USERNAME% to
echo.
echo Cal'C Ver 0.2
echo.
echo ---------------------------
echo.
echo /////////////////////////////////////
echo Your previous Calulated number was %sum%
echo ////////////////////////////////////
echo.
echo Enter the specified alphabet to continue..
echo.
echo a)Addition
echo.
echo b)Subtraction
echo.
echo c)Divison
echo.
echo d)Multipication
echo.
echo e)Square, Cube or any power (by Prof.Pickle)
echo.
set /p do=Value.
if %do%== a goto add
if %do%== A goto add
if %do%== b goto sub
if %do%== B goto sub
if %do%== c goto div
if %do%== C goto div
if %do%== d goto mul
if %do%== D goto mul
if %do%== e goto power1
if %do%== E goto power1
echo.
cls
echo Invalid value = %do%
echo.
pause
cls
goto main

:add
cls
echo ADDITON
echo.
set /p no1="num1. "
echo       +
set /p no2="num2. "
set /a sum=no1+no2
echo ------------
echo %sum%
echo.
pause
cls
goto main


:sub
cls
echo SUBTACTION
echo.
set /p no1="num1. "
echo       -
set /p no2="num2. "
set /a sum=no1-no2
echo ------------
echo %sum%
echo.
pause
cls
goto main


:div
cls
echo DIVISON
echo.
set /p no1="num1. "
echo       /
set /p no2="num2. "
set /a sum=no1/no2
echo ------------
echo %sum%
echo.
pause
cls
goto main


:mul
cls
echo MULTIPLICATION
echo.
set /p no1="num1. "
echo       *
set /p no2="num2. "
set /a sum=no1*no2
echo ------------
echo %sum%
echo.
pause
cls
goto main

:power1
cls
echo Square, Cube or any power (by Prof.Pickle)
echo.
echo Select the number
set /p num=
cls
echo Select the power
set /p pow=
set /a pow=%pow%+1
set ans=%num%
cls
:power2
set /a pow=%pow%-1
if %pow% LSS 1 goto next
set /a ans=%ans%*%num%
goto power2
:next
echo Your answer is %ans%
echo.
echo To open Prof.Pickle's instrutable's profile type 'I'. Type 'M' to goto main
set /p open=
if %open%== I start https://www.instructables.com/member/Prof.+Pickle/
if %open%== i start https://www.instructables.com/member/Prof.+Pickle/
if %open%== m goto main
if %open%== M goto main
goto main

Comments

author
skore1 made it!(author)2012-09-29

Gud work..! Try adding more mathematical functionality like square, cube, average, percentage, etc.. Nice job for a 14 year old.. Keep it up..!

author
code+source made it!(author)2012-09-29

Thanks, I will try for adding more mathematical functionality.

author
MoisesM31 made it!(author)2017-07-04

also thanks instructables for making it possible to post this calculator and the creators of the calculator for posting it and helping me, a 14 year old, learn how to work their way around batch.

author
MoisesM31 made it!(author)2017-07-04

Here is a program used to generate Rubik's cube scrambles in batch. Long but does its job.

:Prompt

ECHO OFF

SET /a s=0

SET /a f=1

SET /a n=0

COLOR 1A

ECHO Pick a scramble of between 1 and 20 moves.

SET /p s="Number of moves of scramble:"

IF 20 LSS %s% (

ECHO Pick a smaller scramble

GOTO Prompt

)

IF %s% LSS 2 (

ECHO Pick a bigger scramble

GOTO Prompt

)

SET /a w=(%RANDOM%*40/32767)+1

GOTO loop

:loop

SET /a m=(%RANDOM%*18/32767)+1

SET /a n=n+1

IF %n% LSS %m% GOTO loop ELSE GOTO sub

:Sub

SET /a a=(%RANDOM%*18/32767)+1

IF "%a%" EQU "1" SET c=R

IF "%a%" EQU "2" SET c=U

IF "%a%" EQU "3" SET c=D

IF "%a%" EQU "4" SET c=F

IF "%a%" EQU "5" SET c=B

IF "%a%" EQU "6" SET c=L

IF "%a%" EQU "7" SET c=R'

IF "%a%" EQU "8" SET c=U'

IF "%a%" EQU "9" SET c=D'

IF "%a%" EQU "10" SET c=F'

IF "%a%" EQU "11" SET c=B'

IF "%a%" EQU "12" SET c=L'

IF "%a%" EQU "13" SET c=R2

IF "%a%" EQU "14" SET c=U2

IF "%a%" EQU "15" SET c=D2

IF "%a%" EQU "16" SET c=F2

IF "%a%" EQU "17" SET c=B2

IF "%a%" EQU "18" SET c=L2

SET /a y=a

SET /a z=a

SET "str=%C%"

ECHO %C%

GOTO Main

:Main

SET /a a=(%RANDOM%*18/32767)+1

SET /a x=a

IF "%a%" EQU "1" (

SET b=R

GOTO Rs

)

IF "%a%" EQU "2" (

SET b=U

GOTO Us

)

IF "%a%" EQU "3" (

SET b=D

GOTO Us

)

IF "%a%" EQU "4" (

SET b=F

GOTO Fs

)

IF "%a%" EQU "5" (

SET b=B

GOTO Fs

)

IF "%a%" EQU "6" (

SET b=L

GOTO RS

)

IF "%a%" EQU "7" (

SET b=R'

GOTO Rs

)

IF "%a%" EQU "8" (

SET b=U'

GOTO Us

)

IF "%a%" EQU "9" (

SET b=D'

GOTO Us

)

IF "%a%" EQU "10" (

SET b=F'

GOTO Fs

)

IF "%a%" EQU "11" (

SET b=B'

GOTO Fs

)

IF "%a%" EQU "12" (

SET b=L'

GOTO RS

)

IF "%a%" EQU "13" (

SET b=R2

GOTO Rs

)

IF "%a%" EQU "14" (

SET b=U2

GOTO Us

)

IF "%a%" EQU "15" (

SET b=D2

GOTO Us

)

IF "%a%" EQU "16" (

SET b=F2

GOTO Fs

)

IF "%a%" EQU "17" (

SET b=B2

GOTO Fs

)

IF "%a%" EQU "18" (

SET b=L2

GOTO RS

)

:Rs

IF "%y%" EQU "1" GOTO Main

IF "%y%" EQU "7" GOTO Main

IF "%y%" EQU "13" GOTO Main

IF "%y%" EQU "6" GOTO Main

IF "%y%" EQU "12" GOTO Main

IF "%y%" EQU "18" GOTO Main

IF "%z%" EQU "1" GOTO Main

IF "%z%" EQU "7" GOTO Main

IF "%z%" EQU "13" GOTO Main

IF "%z%" EQU "6" GOTO Main

IF "%z%" EQU "12" GOTO Main

IF "%z%" EQU "18" GOTO Main

GOTO End

:Us

IF "%y%" EQU "2" GOTO Main

IF "%y%" EQU "8" GOTO Main

IF "%y%" EQU "14" GOTO Main

IF "%y%" EQU "3" GOTO Main

IF "%y%" EQU "9" GOTO Main

IF "%y%" EQU "15" GOTO Main

IF "%z%" EQU "2" GOTO Main

IF "%z%" EQU "8" GOTO Main

IF "%z%" EQU "14" GOTO Main

IF "%z%" EQU "3" GOTO Main

IF "%z%" EQU "9" GOTO Main

IF "%z%" EQU "15" GOTO Main

GOTO End

:Fs

IF "%y%" EQU "4" GOTO Main

IF "%y%" EQU "5" GOTO Main

IF "%y%" EQU "10" GOTO Main

IF "%y%" EQU "11" GOTO Main

IF "%y%" EQU "16" GOTO Main

IF "%y%" EQU "17" GOTO Main

IF "%z%" EQU "4" GOTO Main

IF "%z%" EQU "5" GOTO Main

IF "%z%" EQU "10" GOTO Main

IF "%z%" EQU "11" GOTO Main

IF "%z%" EQU "16" GOTO Main

IF "%z%" EQU "17" GOTO Main

GOTO End

:End

SET /a f=f+1

SET /a z=y

SET /a y=x

SET "Str=%STR% %b%"

ECHO %b%

IF %f% LSS %s% GOTO Main

ECHO Press any key to see scramble

PAUSE

ECHO %STR%

GOTO End2

:End2

ECHO Enter a number

ECHO.

ECHO 1) Regenerate scramble

ECHO.

ECHO 2) Stop generator

SET /p n=" "

IF "%n%" EQU "1" GOTO Prompt

IF "%n%" NEQ "2" GOTO End2

author
KristianN9 made it!(author)2017-04-20

why not just do this:

@echo off
:a
cls
set /p num=Your question:
set /a num=%num%
echo Final nummber=%num%
pause
goto a

author
gv11 made it!(author)2016-11-16

Really nice work But I thinks its a lot of code for not much you could gave shorten it . I did one too (I'm a newbee to batch programming) what do yo think of It ? :

@echo off

mode con: cols=20 lines=10

Title Calculator

:A

echo Enter your operation

set /p cal=

set /a answer=%cal%

echo *** %answer% ***

GOTO A

author
LukeDoesInstructables made it!(author)2016-03-24

Nice! I've made a redesigned version based off yours. It looks even better if you create a shortcut and set font to Consolas 28px and the window size to 25x12:

@echo off

title CMD Calculator

color 8f

:main

cls

echo ------------------------

echo === Calculator V2.0 ====

echo ------------------------

echo Type 'help' for help.

echo.

echo Ans = %ans%

echo.

set /p opr="Operation: "

if %opr%== help goto help1

if %opr%== help2 goto help2

if %opr%== exit exit

if %opr%== + goto sum

if %opr%== - goto sub

if %opr%== * goto mul

if %opr%== / goto div

echo.

echo Invalid operation: '%opr%'

echo Press any key to return.

pause > nul

cls

goto main

:sum

cls

echo ------------------------

echo ========= Sum ==========

echo ------------------------

echo Ans = %ans%

echo ------------------------

set /p n1="Number 1: "

set /p n2="Number 2: "

set /a ans=n1+n2

echo ------------------------

echo %n1% + %n2% = %ans%

echo ------------------------

echo Press any key to return.

pause > nul

goto main

:sub

cls

echo ------------------------

echo ===== Subtraction ======

echo ------------------------

echo Ans = %ans%

echo ------------------------

set /p n1="Number 1: "

set /p n2="Number 2: "

set /a ans=n1-n2

echo ------------------------

echo %n1% - %n2% = %ans%

echo ------------------------

echo Press any key to return.

pause > nul

goto main

:mul

cls

echo ------------------------

echo ==== Multiplication ====

echo ------------------------

echo Ans = %ans%

echo ------------------------

set /p n1="Number 1: "

set /p n2="Number 2: "

set /a ans=n1*n2

echo ------------------------

echo %n1% * %n2% = %ans%

echo ------------------------

echo Press any key to return.

pause > nul

goto main

:div

cls

echo ------------------------

echo ======= Division =======

echo ------------------------

echo Ans = %ans%

echo ------------------------

set /p n1="Number 1: "

set /p n2="Number 2: "

set /a ans=n1/n2

echo ------------------------

echo %n1% / %n2% = %ans%

echo ------------------------

echo Press any key to return.

pause > nul

goto main

:help1

cls

echo ------------------------

echo ========= Help =========

echo ------------------------

echo This is a 32-bit integer

echo calculator, currently

echo capable of doing sum,

echo subtraction, multiplicat

echo ion and division. You ca

echo n type 'exit' to exit.

echo Type 'help2' for page 2.

echo ------- page 01 of 02 --

echo Press any key to return.

pause > nul

goto main

:help2

cls

echo ------------------------

echo ========= Help =========

echo ------------------------

echo You can use +,-,*,/ to

echo determine the operation

echo on the operation field.

echo ------- page 02 of 02 --

echo Press any key to return.

pause > nul

Calc #1.PNGCalc #2.PNG
author
Batchcc made it!(author)2016-01-22

this is nice but I would recommend using if /I because it will allow for a or A with one line.

author
Batchcc made it!(author)2016-01-22

this is nice but I would recommend using if /I because it will allow for a or A with one line.

author
Wast3dHacker made it!(author)2015-02-23

I'm trying to get a working code for factoring. I have this so far:

@echo off

:factor

set a=1

set b=2

set c=3

set d=4

set e=5

set f=6

set g=7

set h=8

set i=9

set j=10

echo Type your number to be factored below...

set /p num1=

set fnum1=num1/a

set fnum2=num1/b

set fnum3=num1/c

set fnum4=num1/d

set fnum5=num1/e

set fnum6=num1/f

set fnum7=num1/g

set fnum8=num1/h

set fnum9=num1/i

set fnum10=num1/j

echo %fnum1%,%fnum2%,%fnum3%,%fnum4%,%fnum5%,%fnum6%,%fnum7%,%fnum8%,%fnum9%,%fnum10%,

pause >nul

---

Except that won't display the answer to the equation, just the value of what the number is set to.

author
Prof.+Pickle made it!(author)2012-10-11

In a reply to the suggested functions that skore1 suggested, the square, cube and other powers would be possible, likewise with average. The percentage is possible but involves ingenuity. Square root and the like would be impossible, in my opinion. Other functions would be easy to add in (such as pi).

In case you are wondering why I am making a new comment (opposed to replying), the "Reply" button has disappeared on select comments. No matter, there are always ways around...

author
code+source made it!(author)2012-10-11

Thanks..
Still doubt, How we can code for square, cube, etc?

author
Prof.+Pickle made it!(author)2012-10-11

Try this: (I skipped "@echo off" and similar codes"

:power1
echo Select the number
set /p num=
cls
echo Select the power
set /p pow=
set /a pow=%pow%+1
set ans=%num%
cls
:power2
set /a pow=%pow%-1
if %pow% LSS 1 goto next
set /a ans=%ans%*%num%
goto power2
:next
echo Your answer is %ans%
pause

If there are any errors in that then I am sorry, I hadn't really thought about making the code earlier; I just knew it was possible.

Remember that if you use this, mention my name.

author
code+source made it!(author)2012-10-12

This code works, I will add to this instructable.
Just one more question, What type of mathematical functionality is this??

author
Prof.+Pickle made it!(author)2012-10-12

This will allow you to square, cube, etc (pretty much any power you want), to any number you want.

author
code+source made it!(author)2012-10-13

cool, I will add to Cal'C programme..

author
Prof.+Pickle made it!(author)2012-12-19

I just figured I'd make a shorter code that accomplishes the same thing:

:order
set /p base=What is the base number? 
set /p order=What is the order? 
for /l %%i in (0,1,%order%) do set ans=%ans% + (%base%*%order%)
echo %ans%



I haven't tested the code but it should work (I know I should but I'm tired).

author
code+source made it!(author)2012-12-22

The code is not working..

author
Prof.+Pickle made it!(author)2012-12-22

I figured it wouldn't. I was tired at the time and hadn't tested it. I'll just be a moment... (Oh, and keep in mind this is a script snippet, not an actual program. I left out basic batch file codes like "@echo off", "title", "color" and most importantly the pause command at the end.)

Here we go, a working script:

@echo off
set ans=0
set /p base=What is the base number?
set /p order=What is the order?
set /a add=%base%*%base%
set /a ans+=add
set /a order-=2
for /l %%i in (1,1,%order%) do set /a ans*=base
echo %ans%
pause



Note: There is a bug with this. The maximum value the order can be for the base of 2 is 30. With the base of 3 it is 29, with 4 it is 28, etc...

I don't know why this happens, but atleast it works.

author
code+source made it!(author)2012-12-22

Awesome! I will add to new version of Cal'c...
Its simple and also effective..

author
Prof.+Pickle made it!(author)2012-12-23

I tried to make it as simple as possible but I know it can be done shorter. Oh well, it'll do for now.

author
Prof.+Pickle made it!(author)2012-10-11

Excellent tutorial; I am sorry I didn't comment on it sooner. As always, let's point out the flaws before rewarding your skill:
Firstly, the version type should always be displayed with an uppercase "V". Also, stating that this is version 0.1 is suggesting that this is still in BETA form. Either change or state somewhere in this instructable that it is a "WIP" (Work-in-progress).
Secondly, on the fifteenth line of your code, there is a grammatical error: The abbreviation "No." is displayed in your code as "no". Please fix this up before someone notices it that doesn't understand.
Thirdly, in your explanation of the code "start", I have spotted a grammatical error. I suggest remodeling it in this fashion: "5>(start)code "start" is used to start an external program.
In fact, you might want to revise most of your explanations as almost all of them contain errors.

Now that my rant is done, shall we get to the pleasantries?

Your calculator is different then others, but the idea is still unoriginal. It is laid out well and easy to understand, and, as always, your tutorial at the start is much appreciated. Just as skore1 said: "Nice job for a 14 year old...".

author
god+squad+101 made it!(author)2012-11-14

I know that was probably just constructive criticism, but... harsh, much?

author
Prof.+Pickle made it!(author)2012-11-14

I hate to sound all stuck up myself, but for the sake of simplicity, here's a metaphor - The parents of bird's push out their younglings in order for them to fly and experience the world. I agree, this is harsh in ways, but can you also see the good in it?

author
code+source made it!(author)2012-10-11

Thanks....
I have edited the mistakes...

author
code+source made it!(author)2012-10-12

ok..

author
skore2 made it!(author)2012-09-29

great tutorial..

About This Instructable

7,198views

2favorites

License:

More by code source:Captcha createrRobotic Arm for disabled personshow to create calculator in batch
Add instructable to: