आज आप पाइथन लैंग्वेज की सहायता से एक simple calculator बनाना सीखेंगे जो उपयोगकर्ता के इनपुट के आधार पर जोड़, घटा, गुणा या भाग आदि कर सकता है।
इस उदाहरण को समझने के लिए, आपको निम्नलिखित Python programming topics का knowledgeहोना चाहिए:
Approach
- हम option 1,2,3,4,5,6 से perform किये जाने वाले operation को choose कर सकते है।
- हम यूजर से दो नंबर ले सकते है। और if, elif, else statement का उपयोग particular operation को execute करने के लिए किया जाता है।
# Python Program make a simple calculator
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
print("5.Modulus")
print("6.Exponentiation")
choice = input("Select operation(1/2/3/4/5/6): ") # take input from the user
if choice in ('1','2','3','4','5','6'):
num1 = float(input("enter your number : ")) # take input from the user
num2 = float(input("enter your number : ")) # take input from the user
if choice == '1':
print("performing 1Add operation \n",num1, "+" ,num2 ,"=", num1 + num2)
elif choice == '2':
print("performing subtract operation \n",num1, "-" ,num2 ,"=", num1 - num2)
elif choice == "3":
print("performing multiply operation \n", num1, "*", num2, "=", num1 * num2)
elif choice == "4":
print("performing division operation \n", num1, "/", num2, "=", num1 / num2)
elif choice == "5":
print("performing modulus operation \n", num1, "%", num2, "=", num1 % num2)
elif choice == "6":
print("performing Exponentiation operation \n", num1, "**", num2, "=", num1 ** num2)
else :
print("Invalid Operation")
OUTPUT
Select operation.
1.Add
2.Subtract
3.Multiply
4.Divide
5.Modulus
6.Exponentiation
Select operation(1/2/3/4/5/6): 3
enter your number : 12
enter your number : 15
performing multiply operation
12.0 * 15.0 = 180.0