Skip to content

Commit fb25ee9

Browse files
authoredAug 30, 2018
Add files via upload
1 parent 8d1ba82 commit fb25ee9

9 files changed

+215
-0
lines changed
 

‎add_two_numbers.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# This program adds two numbers
2+
3+
num1 = 1.5
4+
num2 = 6.3
5+
6+
# Add two numbers
7+
sum = float(num1) + float(num2)
8+
9+
# The below code prints Hello, world!
10+
11+
print('Hello, world!')
12+
13+
# Display the sum
14+
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))

‎leap_year.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Python program to check if the input year is a leap year or not
2+
3+
year = 1999
4+
5+
# To get year (integer input) from the user
6+
# year = int(input("Enter a year: "))
7+
8+
if (year % 4) == 0:
9+
if (year % 100) == 0:
10+
if (year % 400) == 0:
11+
print("{0} is a leap year".format(year))
12+
else:
13+
print("{0} is not a leap year".format(year))
14+
else:
15+
print("{0} is a leap year".format(year))
16+
else:
17+
print("{0} is not a leap year".format(year))

‎matrix_addition_nested_loop.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Program to add two matrices using nested loop
2+
3+
X = [[12,7,3],
4+
[4 ,5,6],
5+
[7 ,8,9]]
6+
7+
Y = [[5,8,1],
8+
[6,7,3],
9+
[4,5,9]]
10+
11+
result = [[0,0,0],
12+
[0,0,0],
13+
[0,0,0]]
14+
15+
# iterate through rows
16+
for i in range(len(X)):
17+
# iterate through columns
18+
for j in range(len(X[0])):
19+
result[i][j] = X[i][j] + Y[i][j]
20+
21+
for r in result:
22+
print(r)

‎natural_sum_recursion.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Python program to find the sum of natural numbers up to n using recursive function
2+
3+
def recur_sum(n):
4+
"""Function to return the sum
5+
of natural numbers using recursion"""
6+
if n <= 1:
7+
return n
8+
else:
9+
return n + recur_sum(n-1)
10+
11+
# change this value for a different result
12+
num = 16
13+
14+
# uncomment to take input from the user
15+
#num = int(input("Enter a number: "))
16+
17+
if num < 0:
18+
print("Enter a positive number")
19+
else:
20+
print("The sum is",recur_sum(num))

‎simple_calculator.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# This function adds two numbers
2+
def add(x, y):
3+
return x + y
4+
5+
6+
# This function subtracts two numbers
7+
def subtract(x, y):
8+
return x - y
9+
10+
11+
# This function multiplies two numbers
12+
def multiply(x, y):
13+
return x * y
14+
15+
16+
# This function divides two numbers
17+
def divide(x, y):
18+
return x / y
19+
20+
21+
print("Select operation:")
22+
print("1.Add")
23+
print("2.Subtract")
24+
print("3.Multiply")
25+
print("4.Divide")
26+
27+
# Take input from the user
28+
choice = input("Enter choice(1/2/3/4):")
29+
30+
num1 = int(input("Enter first number: "))
31+
num2 = int(input("Enter second number: "))
32+
33+
if choice == '1':
34+
print(num1, "+", num2, "=", add(num1, num2))
35+
36+
elif choice == '2':
37+
print(num1, "-", num2, "=", subtract(num1, num2))
38+
39+
elif choice == '3':
40+
print(num1, "*", num2, "=", multiply(num1, num2))
41+
42+
elif choice == '4':
43+
print(num1, "/", num2, "=", divide(num1, num2))
44+
else:
45+
print("Invalid input")

‎simple_calculator_in_loop.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
def simc():
2+
def add(a,b):
3+
z=a+b
4+
print("Sum is %d" % z)
5+
def sub(a,b):
6+
return a-b
7+
def mul(a,b):
8+
return a*b
9+
def div(a,b):
10+
return a/b
11+
def rem(a,b):
12+
return a%b
13+
14+
print("Simple Calculator")
15+
while(1):
16+
print("1.Add\n2.Subtract\n3.Multiply\n4.Divide\n5.Remainder\n6.Exit")
17+
ch = int(input("Enter your choice: "))
18+
if (ch == 6):
19+
print("Thank you and Exiting")
20+
break
21+
else:
22+
a = int(input("Enter first number: "))
23+
b = int(input("Enter second number: "))
24+
if(ch==1):
25+
add(a,b)
26+
elif (ch==2):
27+
z=sub(a,b)
28+
print("Difference is %d"%z)
29+
elif(ch==3):
30+
z=mul(a,b)
31+
print("Product is %d"%z)
32+
elif (ch==4):
33+
z=div(a,b)
34+
print("Qoutient is %f"%z)
35+
elif (ch==5):
36+
z=rem(a,b)
37+
print("Remainder is %d"%z)
38+
else:
39+
print("Invalid input!")
40+
ch=input("Continue? (y/n): ")
41+
if(ch=="y"):
42+
continue
43+
elif(ch=="n"):
44+
break
45+
else:
46+
print("Invalid input!\nExiting.")
47+
break

‎solve_quadratic_equation.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Solve the quadratic equation ax**2 + bx + c = 0
2+
3+
# import complex math module
4+
import cmath
5+
6+
a = 1
7+
b = 5
8+
c = 6
9+
10+
# To take coefficient input from the users
11+
# a = float(input('Enter a: '))
12+
# b = float(input('Enter b: '))
13+
# c = float(input('Enter c: '))
14+
15+
# calculate the discriminant
16+
d = (b**2) - (4*a*c)
17+
18+
# find two solutions
19+
sol1 = (-b-cmath.sqrt(d))/(2*a)
20+
sol2 = (-b+cmath.sqrt(d))/(2*a)
21+
22+
print('The solution are {0} and {1}'.format(sol1,sol2))

‎sort_words_in_alphabetical_order.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Program to sort alphabetically the words form a string provided by the user
2+
3+
# change this value for a different result
4+
my_str = "Hello this Is an Example With cased letters"
5+
6+
# uncomment to take input from the user
7+
#my_str = input("Enter a string: ")
8+
9+
# breakdown the string into a list of words
10+
words = my_str.split()
11+
12+
# sort the list
13+
words.sort()
14+
15+
# display the sorted words
16+
17+
print("The sorted words are:")
18+
for word in words:
19+
print(word)

‎square_root.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Python Program to calculate the square root
2+
3+
# Note: change this value for a different result
4+
num = 8
5+
6+
# uncomment to take the input from the user
7+
#num = float(input('Enter a number: '))
8+
num_sqrt = num ** 0.5
9+
print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))

0 commit comments

Comments
 (0)
Please sign in to comment.