Skip to content

Commit b54b270

Browse files
Code
1 parent 0f25e4e commit b54b270

File tree

2 files changed

+72
-15
lines changed

2 files changed

+72
-15
lines changed

Calculator.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Simple Calculator By Using Function.
2+
3+
# This Function Adds Two Numbers
4+
def add(x, y):
5+
return x + y
6+
7+
# This Function Subtracts Two Numbers
8+
def subtract(x, y):
9+
return x - y
10+
11+
# This Function Multiplies Two Numbers
12+
def multiply(x, y):
13+
return x * y
14+
15+
# This Function Divides Two Numbers
16+
def divide(x, y):
17+
return x / y
18+
19+
# This Function Modulus Two Numbers
20+
def modulus(x, y):
21+
return x % y
22+
23+
print("Select Operation:-")
24+
print("1. Add")
25+
print("2. Subtract")
26+
print("3. Multiply")
27+
print("4. Divide")
28+
print("5. Modulus")
29+
30+
while True:
31+
choice = input("Enter Choice(1/2/3/4): ")
32+
33+
if choice in ('1', '2', '3', '4'):
34+
num1 = float(input("Enter First Number: "))
35+
num2 = float(input("Enter Second Number: "))
36+
37+
if choice == '1':
38+
print(num1, "+", num2, "=", add(num1, num2))
39+
40+
elif choice == '2':
41+
print(num1, "-", num2, "=", subtract(num1, num2))
42+
43+
elif choice == '3':
44+
print(num1, "*", num2, "=", multiply(num1, num2))
45+
46+
elif choice == '4':
47+
print(num1, "/", num2, "=", divide(num1, num2))
48+
49+
elif choice == '5':
50+
print(num1, "%", num2, "=", modulus(num1, num2))
51+
52+
next_calculation = input("Let's Do Next Calculation...? (Yes/No): ")
53+
if next_calculation == "no":
54+
break
55+
56+
else:
57+
print("Invalid Input")
58+

Square_Root.py

+14-15
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
# Python Program to find the Square Root.
2-
# 1st Method (Using Exponentiation)
3-
# num = 64
4-
# sr = num**(1/2)
5-
# print("The Square Root of the Given Number is: ", sr)
1+
# Write a Python Program to Find Square Root.
2+
def mySqrt(x):
3+
left = 1
4+
right = x
5+
mid = 0
6+
while(left <= right):
7+
mid = (left + right) // 2
8+
if mid * mid > x:
9+
right = mid - 1
10+
else:
11+
left = mid + 1
12+
sqrt = mid
13+
return sqrt
614

7-
# 2nd Method (With User Inputs)
8-
# num = int(input("Enter a Number Here: "))
9-
# sr = num**(0.5)
10-
# print("The Square Root of the Given Number is: ", sr)
11-
12-
# 3rd Method (Using Math Module)
13-
import math
14-
num = int(input("Enter a Number Here: "))
15-
sr = math.sqrt(num)
16-
print("The Square Root of the Given Number is", sr)
15+
print(mySqrt(36))

0 commit comments

Comments
 (0)