15 March 2016

How to Create a Simple Calculator With Batch Program

Leave a Comment
The first thing you'll learn in every programming language is how to create a hello world program. This will explain all basic commands to print texts. Next to this, you'll learn about basic maths like how to do addition or subtraction. Every single program works on maths A-B calculations. I might not be wrong If I will say that Maths is the building block of all programs. In almost all programming languages, you can easily create your own calculator and in today's tutorial, I will guide you how to create that simple calculator in batch language.

Create Calculator With Batch Program

Before moving to the steps, first let me introduce you with batch programming. In simple words, Batch is a scripting language which works in Windows, DOS and OS 2 platform and can be programmed in notepad or simply in windows command prompt shell. It consist of simple commands to be executed by command line interpreter. The file extension type of batch program is '.bat'.

Steps to Create Batch Program


Now to create this program, you've to do some programming in batch language. Don't worry! I had already created this code for you. Simply copy this and then proceed with below steps.

@echo off
title My First Calculator
color 0a
:top
cls
echo Welcome to Batch Calculator
set /p fn=Please enter first number:
set /p sn=Now enter second number:
set /p math=What you what to do, type any of the operator ( -, +, * or /):
set /a ans=%fn%%math%%sn%
echo Your answer is: %ans%
pause>nul
echo Done!
echo Press any key to start again!
pause>nul
goto top
  1. First of all, open notepad from start menu. You can also use latest notepad ++.
  2. Now copy above code inside it and press "CTRL+S" keyboard shortcut key to save it. Alternatively, you can click on "File>Save as" button.
  3. After this, save dialog box will open. Here choose the location where you want to save this program and type your file name with ".bat" as suffix.

    For example, "calculator.bat". Here "calculator" is the file name and ".bat" is the file format. Without file format, this program will not work. So make that you've typed correct name.


    Save Batch Program

  4. Finally click on "Save" button and that's it.
Now let me explain you the basic logic behind this program. The most important command in above code is set command. It will take input from user and then save in it variable. For example, fn, sn, math and ans are the variables in this program which will take input from user through set command. Now to print that input, simply enclose them under percent mark as like this "%fn%".

For all calculations purpose, "set /a ans=%fn%%math%%sn%" code is responsible. As you can see, here I had declared a new variable named as "ans" which is equal to the sum of first number, operator and second number. For example, if first number is 10, operator is "+" (plus or addition) and second number is 20 then the value of ans variable will be 30 which will be printed later as "%ans%". Still don't get it? Ask me in comment box. Except these commands, all other are basic and here is a short description for all of them.


CommandExplanation
echoecho is used to print text
titleThis will add title to your program
color 0aThis is used to set background and text color
pause>nulThis command will pause screen until user press any key.

How to Use This Calculator


After saving this program, its time to use it. Simply go to the location where you have saved this batch program and open it. It will display a welcome note as shown in below picture.

Batch Calculator

Now type first number and choose maths operator ( Note: only 4 operators as acceptable which includes plus, minus, divide and multiply). Finally type second number and hit ENTER button. Within seconds, it will show you the correct answer. Isn't that cool? Hm. Looks like it worked for you. You're now an advanced programmer, LOL.

Jokes apart, there are few important things which you should keep in mind while using this calculator. In this post title, I had used a term "simple calculator". So basically, this calculator can only perform basic maths calculations like multiplication, subtraction, addition and division. You can't use trigonometric and exponential functions.

Apart from this, if you'll type a long value like 1000000000000 then it will display an error message as "Invalid number. Numbers are limited to 32-bit of precision". On the other hand, you can't use fractional numbers like this - 100.05. This will again show an error message as "Invalid operator". Make sure that you avoid all these errors.

Now it's your turn to try this. Go and create your own calculator. If you've any doubts, fell free to ask me in below comment box. I am always here to help you. Love and blessings!

Leave A Comment