Math Functions in Batch Programming


In batch programming, you cannot use real numbers, or numbers with decimals. There are other words with numbers with decimals, like float point. In batch programming, you can only use integer or whole numbers, or numbers without decimals. For example numbers, like 1,2,3,100,1000,100000,10^13, etc. If you divide a number, a lot of times you get a numbers with decimals. In batch programming, you will get back a rounded whole number.

We have some math operators:
Multiplication : *
Division : /
Subtraction : –
Addition : +
And : &
Or : |
Xor : ^

@echo off
set /a a=2
set /a b=3
set /a c=a+b
pause

Output:
2
3
5

(I set a as 2, and I set b as 3. Then, I set c to be a + b, so you get 5 at the end.)

@echo off
:loop
echo Please enter a number for A
set /p A=
echo Please enter a number for B
set /p B=
set /a C=A+B
set /a D=A*B
echo The answer for the sum of A and B is %C%
echo The answer for the product of A and B is %D%
pause
goto loop

Output: Please enter a number for A
Input: 3
Output: Please enter a number for B
Input: 4
Output: The answer for the sum of A and B is 7
Output: The answer for the product of A and B is 12

(In the simple script I wrote, the user can add A and B. Then, the user gets a result of the sum of two numbers, and the product of A & B. After the program is finished, it goes to loop which repeat the program again.)