Python Codes | Codes to make calculater | Codes to make Tables | Code to find factorial of numbers | Computer Science | CS

 CODE 1: TO MAKE A CALCULATER IN PYTHON

def add(x, y):

return x+y

def subtract(x, y):

return x-y

def multiply (x, y):

return x*y

def divide (x, y):

return x/y

print("select operation")

print("1. Add")

print("2. Subtract")

print("3. Multiply")

print("4. Divide")

while True:

choice=input("Enter choice (1/2/3/4):  ")

if choice in ('1','2','3','4'):

num1=float(input("enter first number:    "))

num2=float(input("enter second number:    "))

if choice  == '1':

print(num1, "+", num2, "=", add(num1, num2))

elif choice == '2':

print(num1, "-", num2,"=", subtract(num1, num2))

elif choice == '3':

print(num1, "×", num2, "=", multiply(num1, num2))

elif choice == '4':

print(num1, "÷", num2, "=", divide(num1, num2))

break

else:

print("Invalid Input")


______________________________________________

CODE 2: TO MAKE A CALCULATER IN PYTHON {2}


result=0

val1=float(input("Enter value 1; "))

val2=float(input("Enter value 2; "))

op = input("Enter any one of the operator (+,-,*,/); " )

if op == "+":

result= val1 + val2

elif op=="-":

if val1 > val2:

result= val1 - val2

else:

result= val2 - val1

elif op=="*":

result= val1 * val2

elif op=="/":

if val2 == 0:

print("Error! Division by zero is not allowed. Program terminated")

else:

result= val1/val2

else:

print("Wrong input, program terminated")

print("The result is ",result)


______________________________________________

To run the code: just select the code and paste in your python
______________________________________________

CODE 3: CODES TO MAKE TABLE IN PYTHON

n=int(input("enter the number:"))
for i in range(1,11):
print(n,'×', i, '=', n*i)

______________________________________________

CODE 4: CODES TO GET FACTORIAL OF ANY NUMBER IN PYTHON

# Python program to find the factorial of a number provided by the user.

num = int(input("enter no. :"))

factorial = 1

# check if the number is negative, positive or zero
if num < 0:
   print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
   print("The factorial of 0 is 1")
else:
   for i in range(1,num + 1):
       factorial = factorial*i
   print("The factorial of",num,"is",factorial)

______________________________________________

To run the code: just select the code and paste in your python

______________________________________________








Comments

Popular posts from this blog

Class 12th Physics and Chemistry Handwritten Notes... ⚗️⚖️⚛️

Electric Charge And Field | Chap 1 | Class 12th | Physics | Handwritten Notes

The Solid State | Chap 1 | Class 12th Chemistry | Handwritten Notes

Haloalkanes & Haloarenes | Chap 10 | Class 12th | Organic Chemistry | Handwritten Notes

My SQL | Short Notes | Class 12 | CS | Computer Science