How to Create Calculator in Batch

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

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Hack It! Contest

Participated in the
Hack It! Contest

1 Person Made This Project!

Recommendations

  • Backyard Contest

    Backyard Contest
  • Silly Hats Speed Challenge

    Silly Hats Speed Challenge
  • Finish It Already Speed Challenge

    Finish It Already Speed Challenge

36 Discussions

0
skore1
skore1

7 years ago on Introduction

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

0
code source
code source

Reply 7 years ago on Introduction

Thanks, I will try for adding more mathematical functionality.

0
lackmey
lackmey

5 months ago

so uh... 10^90=-2147483648?

0
Waquar Ahsab
Waquar Ahsab

6 months ago

It showed 2 square = 8!!!!

0
MoisesM31
MoisesM31

2 years ago

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

0
Waquar Ahsab
Waquar Ahsab

Reply 6 months ago

Can you tell the command EQU meaning?

0
Batchcc
Batchcc

4 years ago

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

0
Waquar Ahsab
Waquar Ahsab

Reply 6 months ago

Yes you are right

0
MoisesM31
MoisesM31

2 years ago

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.

0
Waquar Ahsab
Waquar Ahsab

Reply 6 months ago

Awesome! Your's is better than other's!

0
jorgemath
jorgemath

1 year ago

Excellent tutorial.

0
JernejR
JernejR

2 years ago

i made calculator for calculating resistor resistance. and i cant make it to calculate this: 6,6/0,02 and the output is 0...

my code:

@echo off

cls

set /a bv=0

set /a lv=0

set /a la=0

set /a res=0

set /a pres=0

title Kalkulator za upor

:a

cls

echo Vnesite napetost vira (V):

set /p bv=

echo Vnesite napetost za LED (V):

set /p lv=

echo Vnesite tok za LED (A):

set /p la=

goto calc

:calc

set /a pres=bv-lv

set /a res=pres/la

goto d

:d

cls

echo POTREBEN UPOR: %res%

pause >nul

goto a

and when i put in 10 2 2 is output 4 which is correct. but when i put in 9 2,4 0,02 is output 0 instead of 330...pls help

0
JernejR
JernejR

Reply 2 years ago

i mean when i put in 10 for bv and 2 for lv and 2 for la.....and same with 9 2,4 0,02..

0
gv11
gv11

3 years ago

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

0
BinT1
BinT1

Reply 2 years ago

wow!..thnx man..this was exactly what i was looking for!

0
KristianN9
KristianN9

3 years ago

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

0
Batchcc
Batchcc

4 years ago

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

0
Wast3dHacker
Wast3dHacker

5 years ago on Introduction

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.

0
Prof. Pickle
Prof. Pickle

7 years ago on Introduction

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...