❌

Reading view

There are new articles available, click to refresh the page.

A simple design calculator on linux command line

when we open terminal or Konsole in Linux we used to think by using many utilities why calculators was not there interactively in itΒ ?

Now here is the solution for it just by cloning a repository from GitHub on your PC. Here is the GitHub link to cloneΒ it.

Just open your terminal and make a directory for safety purpose for thatΒ type,

mkdir calculator;cd calculator

creating directory on my home directory and changing it to calculator

the above command explain about making and changing directory from home directory.

then type,

git clone https://github.com/vishnumur777/simplecalculatorbash

cloning repository through gitΒ command

this command will clone all the files present in the repository.

but please make sure that you have installed git on yourΒ PC.

Then change directory to simplecalculatorbash using

cd simplecalculatorbash/

changing directory to simplecalculatorbash

then type,

chmod +xΒ calci.sh

modifying permissions to executables

this command changes the permission to run on user without depending on rootΒ . So that many PC cause this error that permission was denied while executing theΒ file.

For execution runΒ type,

./calci.sh

command to execute simplecalculator

and press enterΒ key

which will run very colourful calculator interactive prompt by installing dependencies which I posted onΒ GitHub.

calculator performing addition of two numbers interactively

CALCULATOR

<html>
<head>
    <title>Simple Calculator</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="author" content="Sugirtha">
    <meta name="description" content="Calculator using HTML, CSS, JavaScript">   
    <link rel="stylesheet" href="calc.css">
</head>
<body>
    <h2 align="center">CALCULATOR</h2>
<div class="borderContainer">
<div class="container" >
<div class="resultsContainer" ><input type="text" id="results" name="results" class="result" readonly></div>
<div class="numPadContainer">
    <p>
    <input type="button" value="7" class="button black" onclick='calc("7")'>
    <input type="button" value="8" class="button black" onclick='calc("8")'>
    <input type="button" value="9" class="button black" onclick='calc("9")'>
    <input type="button" value="/" class="button opr" onclick='calc("/")'>
    </p>
    <p>
    <input type="button" value="4" class="button black" onclick='calc("4")'>
    <input type="button" value="5" class="button black" onclick='calc("5")'>
    <input type="button" value="6" class="button black" onclick='calc("6")'>
    <input type="button" value="*" class="button opr" onclick='calc("*")'>
   </p>
    <p>
    <input type="button" value="1" class="button black" onclick='calc("1")'>
    <input type="button" value="2" class="button black" onclick='calc("2")'>
    <input type="button" value="3" class="button black" onclick='calc("3")'>
    <input type="button" value="-" class="button opr" onclick='calc("-")'>
    </p>
    <p>
    <input type="button" value="0" class="button black" onclick='calc("0")'>
    <input type="button" value="00" class="button black" onclick='calc("00")'>
    <input type="button" value="." class="button black" onclick='calc(".")'>
    <input type="button" value="+" class="button opr" onclick='calc("+")'>

    </p>
    <p>
    <input type="button" value="mod" class="button opr" onclick='calc("%")'>
    <input type="button" value="C" class="button red" onclick='results.value=""'>
    <input type="button" value="=" class="button orange" onclick='doOper()' >

   </p>
</div>
</div>
</div>
<script>
    var answered=false;
    function calc(val) 
    {
        optrs = "+-*/%"
        if (answered && !optrs.includes(val)) 
        {
         
                //alert("not included opr - clearing ans")
                document.getElementById("results").value="";
                
            
        }
        document.getElementById("results").value += val;
        answered=false;
    }
    function doOper()
    {
        try
        {
            dispAns(eval(document.getElementById("results").value));
            
        }
        catch
        {
            dispAns("Input Error");
        }
    }
    function dispAns(output) 
    {
        document.getElementById("results").value = output;
        answered=true;
    }

</script>
</body>
</html>
body 
{
    background-color:black;
    color:lightgrey;
}



.borderContainer 
{
    position:relative;
    top:10px;
    left:36%;
    width:370;
    height:500;
    background-color: lightgrey;
}
.container
{
    position:relative;
    left:8%;
    top:25px;
    width:310px;
    height:450px;
    background-color:#3d4543;
    border-radius:3px;
}
.numPadContainer
{
    position:relative;
    left:25px;
    top:40px; 
}
.result
{
    position:relative;
    left:24px;
    top:28px;
    background-color:#bccd95;
    color:black;
    text-align:right;
    height:40px;
    width:260px;
    border-radius:10px;
    border-color:white;
}
.button
{
    position:relative;
    color:white;
    border:none;
    border-radius:8px;
    cursor:pointer;
    width:48px;
    height:42px;
    margin-left:10px;
}
.button.black
{
    background-color:#303030; 
    border-radius:8px;
    border-bottom:black 2px solid;  
    border-top:2px 303030 solid; 
}
.button.red
{
    background-color:#cf1917;  
    border-bottom:black 2px solid; 
    font-weight:bold;
}
.button.opr
{
    background-color:grey;  
    border-bottom:black 2px solid; 

}
.button.orange
{
    background-color:orange;
    border-bottom:black 2px solid;
    width:110px;
    font-weight:bold;
}

OUTPUT:

Let's make a Calculator using Py.

Before we actually make a calculator, let's first see some basic mathematical expressions...

1. Add

num1 = 2
num2 = 3
print(num1+num2) 

5

2. Subtract

num1 = 7
num2 = 5
print(num1-num2)

2

3. Multiply

num1 = 5
num2 = 5
print(num1*num2)

25

4. Divide

num1 = 100
num2 = 5
print(num1/num2)

20

5. Modulus (nothing but remainder)

quotient = 5//2
remainder = 5 % 2
print(quotient , "," ,remainder)

2 , 1

6.Exponentiate (powers)

For ex; a power b in python written as a**b

num1 = 3
num2 = 3
print(num1**num2)

27

Input Datatype and Typecasting

# Addition

num1 = int(input("Enter Number 1 : "))
num2 = int(input("Enter Number 2 : "))

result = num1+num2
print("Result is : ", result)

Enter Number 1 : 1
Enter Number 2 : 4
Result is :  5

Here it appears as steps but I'm unable to present it.
You can just do it and see.

# to make it possible in decimals too we use float

num1 = float(input("Enter Number 1 : "))
num2 = float(input("Enter Number 2 : "))

result = num1+num2
print("Result is : ", result) 

Enter Number 1 : 1.5
Enter Number 2 : 2.5
Result is :  4.0

Similarly, we do for other operations.

Now, with this knowledge we gonna create a simple calculator by using the following code;

print("Simple Calculator")
print("Select Operation : ")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Modulus")
print("6. Exponentiate")

choice = input("Enter choice (1/2/3/4/5/6) : ")
num1 = float(input("Enter first  Number : "))
num2 = float(input("Enter second Number : "))

if choice == "1" :
   result = num1 + num2
   print(result)
elif choice == "2" :
   result = num1 - num2
   print(result)
elif choice == "3" :
   result = num1 * num2
   print(result)
elif choice == "4" :
   result = num1 / num2
   print(result)
elif choice == "5" :
   result = num1 % num2
   print(result)
elif choice == "6" :
   result = num1 ** num2
   print(result)
else :
   print("option not available")

So, that's what I learned under this topic.
You can use the same code above and chk whether it works for you too..

Platforms I use to try these codes :

  • W3 Schools Tryit Editor
  • Google Colaboratory
  • Visual Studio Code

.....

First Python Program

while "True":

    print ("CALCULATOR")
    print ("Select Operation:")
    print ("1. Add")
    print ("2. Subtract")
    print ("3. Multiplication")
    print ("4. Division")
    print ("5. Modulus")
    print ("6. Exponentiate")

    choice = input("Enter Choice (1/2/3/4/5/6/7): ")

    if choice == "7":
       print ("Thank you for using calculator")

    elif choice < "7":

         num1 = float(input("Enter First Number:"))
         num2 = float(input("Enter Second Number:"))

    elif choice > "7":
         print ("Invalid Choice")

    # Addition

    if choice == "1":
       result = num1 + num2
       print ("result is: ", result)

    # Subtraction

    elif choice == "2":

       result = num1 - num2
       print ("result is: ", result)

    #Multiplication

    elif choice == "3":
       result = num1 * num2
       print ("result is: ", result)

    # Division


    elif choice == "4":
       result = num1 / num2
       print ("result is: ", result)
    # Remainder

    elif choice == "5":
       result = num1 % num2
       print ("result is: ", result)


    elif choice == "6":
       result = num1 ** num2
       print ("result is: ", result)
❌