The Wayback Machine - https://web.archive.org/web/20240201104948/https://www.geeksforgeeks.org/calculator-program-with-batch-scripting-in-windows/
Open In App
Related Articles

Calculator Program with Batch Scripting in Windows

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

By using the Batch Scripting of command prompt one could even design a normal basic calculator that takes expressions and returns the result. Type the following script in notepad or any other text editor software:

@echo off
:a
echo ________Calculator________
echo.
set /p expression= Enter expression to calculate:
set /a ans=%expression%
echo.
echo = %ans%
echo
pause
cls
goto a

After writing the above text in a text editor, save the file with the extension “ .bat “. To run the program, simply double-click on the saved file, the program will open and the code will get executed in Command Prompt. It should look something like:

Welcome screen!

Code Explanation and Test Cases:

  • @echo off — signals Command Prompt to not display the commands (code).
  • : a — the signals starting point of a loop.
  • echo — same as print/println/printf/etc. Displays the specified message as output to the screen.
  • echo. — will output a blank line.
  • set — used to set value against a variable.
  • set /p expression — /p signals that this is prompt. “Expression” is just a variable name.
  • set /a ans – /a signal that variable has a numerical value. “ans” is the variable name.
  • %ans% — calls the variable “ans”
  • pause — will pause/ stop the program flow until the user will press a key (any key) and continue the program flow only when the users press a key (any).
  • cls – clears the screen
  • goto a- signals to go to the start point of the loop, effectively looping our calculator program so it can take in and calculate new expression.

Program Test Cases:

  • The program can handle simple expression calculations like:

Calculating Simple Expression

  • And it can also handle complex expressions, such as:

Calculating Complex Expression

Note:-

  • The above program is calculating expressions by utilizing the calculator (that can perform simple arithmetic on 32-bit signed integers) that comes with Command Prompt.
  • Since we are utilizing built-in calculator of Command Prompt, we can only perform following expression: + (addition), – (subtraction), * (multiplication),  / (divide), % (modulo).

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!


Last Updated : 07 Sep, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads