-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from Anirbansaha007/main
Created calculator.py
- Loading branch information
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
num = int(input("Enter a number: ")) | ||
|
||
factorial = 1 | ||
|
||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
nterms = int(input("How many terms? ")) | ||
|
||
n1, n2 = 0, 1 | ||
count = 0 | ||
|
||
if nterms <= 0: | ||
print("Please enter a positive integer") | ||
elif nterms == 1: | ||
print("Fibonacci sequence upto",nterms,":") | ||
print(n1) | ||
else: | ||
print("Fibonacci sequence:") | ||
while count < nterms: | ||
print(n1) | ||
nth = n1 + n2 | ||
# update values | ||
n1 = n2 | ||
n2 = nth | ||
count += 1 |