How to Create Calculator in Batch

8,673

2

32

Posted

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

Share

Recommendations

  • Epilog Challenge 9

    Epilog Challenge 9
  • Sew Warm Contest 2018

    Sew Warm Contest 2018
  • Gluten Free Challenge

    Gluten Free Challenge
user

We have a be nice policy.
Please be positive and constructive.

Tips

Questions

30 Comments

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

Thanks, I will try for adding more mathematical functionality.

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

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

user

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

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

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.

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

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