how to write python programs for arithmetic operations

 Arithmetic operations

Arithmetic operations are the basic operation in any programming language. Even in mathematics, these are the basic things that everybody should know. They are

  • Addition(+) 
  • Subtraction(-)
  • Multiplication(*)
  • Division(/)
  • Modulus(%)
  • Exponent(**)
  • Floor division(//)
Program 1 :Addition of three numbers.

I am using notepad as a editor to write this python program. I named this file name as "add3.py"
The program is

a=2

b=3

c=4

print(“the sum of three numbers are”+a+b+c)

 The output is shown below.

C:\Users\python>py add3.py

the sum of three numbers are 9

 C:\Users\python>


Program 2: Subtraction of two numbers 

The program "sub.py"

d=10
e=4
print(" The subtraction  value is",d-e)

The output is given below
C:\Users\python>py sub.py
 The subtraction  value is 6

C:\Users\python>

Program 3: The multiplication table 
Program "mul.py"
a=2
for i in range(1,11):
print(a,'x',i,'=',a*i)

The output is
C:\Users\python>py mul.py
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20

C:\Users\python>


Program 4: Sum of the digit
Program "sum.py"
In this program, We used  modulus operator(%), Floor division operator(//) and division operator(/).

m=int(input("enter  the number"))
c=0
while(m>0):
 sum=m%10
 c=c+sum
 m=m//10
print("The sum of the digit is:",c)

The output is given below:
C:\Users\python>py sum.py
enter  the number567
The sum of the digit is: 18

C:\Users\python>

Program 5: Square  of a number

Program "square.py"-This program uses exponent operator

a=5
square=a**2
print(" The square value of the number is",square)

The output is:

C:\Users\python>py square.py
 The square value of the number is 25

C:\Users\python>

These are the simple programs using arithmetic operations.

No comments:

Post a Comment