Skip to content

Commit 0b6adf5

Browse files
Add files via upload
0 parents  commit 0b6adf5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+861
-0
lines changed

Diff for: Milestone 1/Conditionals and Loops/Calculator.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Write a program that performs the tasks of a simple calculator.
2+
# The program should first take an integer as input and
3+
# then based on that integer perform the task as given below.
4+
# 1. If the input is 1, then 2 integers are taken from the user and
5+
# their sum is printed.
6+
# 2. If the input is 2, then 2 integers are taken from the user and
7+
# their difference(1st number - 2nd number) is printed.
8+
# 3. If the input is 3, then 2 integers are taken from the user and
9+
# their product is printed.
10+
# 4. If the input is 4, then 2 integers are taken from the user and
11+
# the quotient obtained (on dividing 1st number by 2nd number) is printed.
12+
# 5. If the input is 5, then 2 integers are taken from the user and
13+
# their remainder(1st number mod 2nd number) is printed.
14+
# 6. If the input is 6, then the program exits.
15+
# 7. For any other input, then print "Invalid Operation".
16+
17+
while True:
18+
i = int(input())
19+
if(i == 1):
20+
a = int(input())
21+
b = int(input())
22+
print(a+b)
23+
elif(i == 2):
24+
a = int(input())
25+
b = int(input())
26+
print(a-b)
27+
elif(i == 3):
28+
a = int(input())
29+
b = int(input())
30+
print(a*b)
31+
elif(i == 4):
32+
a = int(input())
33+
b = int(input())
34+
print(a/b)
35+
elif(i == 5):
36+
a = int(input())
37+
b = int(input())
38+
print(a % b)
39+
elif(i == 6):
40+
break
41+
else:
42+
print("Invalid Operation")

Diff for: Milestone 1/Conditionals and Loops/Checknumber.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Given an integer n, find if n is positive, negative or 0.
2+
# If n is positive, print "Positive"
3+
# If n is negative, print "Negative"
4+
# And if n is equal to 0, print "Zero".
5+
6+
n = int(input())
7+
8+
if(n > 0):
9+
print("Positive")
10+
elif(n == 0):
11+
print("Zero")
12+
else:
13+
print("Negative")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Given three values - Start Fahrenheit Value (S), End Fahrenheit value (E) and Step Size (W),
2+
# you need to convert all Fahrenheit values from Start to End at the gap of W,
3+
# into their corresponding Celsius values and print the table.
4+
5+
S = int(input())
6+
E = int(input())
7+
W = int(input())
8+
9+
while E >= S:
10+
c = (5*(S-32))/9
11+
d = int(c)
12+
print(S, d)
13+
S = S+W
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Nth term of Fibonacci series F(n), where F(n) is a function,
2+
# is calculated using the following formula -
3+
# F(n) = F(n-1) + F(n-2),
4+
# Where, F(1) = 1,
5+
# F(2) = 1
6+
# Provided N you have to find out the Nth Fibonacci Number.
7+
8+
def fact(n):
9+
if n==1:
10+
return 1
11+
elif n==2:
12+
return 1
13+
else:
14+
sum = 0
15+
a = 1
16+
b = 1
17+
while n-1 != 0:
18+
sum += a
19+
a = b
20+
b = sum
21+
n=n-1
22+
return sum
23+
24+
n = int(input())
25+
print(fact(n))
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Write a program to determine if given number is palindrome or not.
2+
# Print true if it is palindrome, false otherwise.
3+
# Palindrome are the numbers for which reverse is exactly same as the original one.
4+
# For eg. 121
5+
6+
def checkPalindrome(num):
7+
n = num
8+
sum = 0
9+
while(n != 0):
10+
r = n%10
11+
sum = sum*10+r
12+
n = n//10
13+
return sum
14+
15+
16+
num = int(input())
17+
isPalindrome = checkPalindrome(num)
18+
if(isPalindrome == num):
19+
print('true')
20+
else:
21+
print('false')
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Write a program to generate the reverse of a given number N.
2+
# Print the corresponding reverse number.
3+
# Note : If a number has trailing zeros, then its reverse will not include them.
4+
# For e.g., reverse of 10400 will be 401 instead of 00401.
5+
6+
N = int(input())
7+
sum = 0
8+
while(N != 0):
9+
r = N % 10
10+
sum = sum*10+r
11+
N = N//10
12+
print(sum)
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Given a number N, print sum of all even numbers from 1 to N.
2+
3+
N = int(input())
4+
sum = 0
5+
if(N % 2 == 0):
6+
while(N != 0):
7+
sum = sum+N
8+
N = N-2
9+
else:
10+
N = N-1
11+
while(N != 0):
12+
sum = sum+N
13+
N = N-2
14+
print(sum)
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Write a program to input an integer N and print the sum of all its even digits and
2+
# sum of all its odd digits separately.
3+
# Digits mean numbers, not the places!
4+
# That is, if the given integer is "13245", even digits are 2 & 4 and odd digits are 1, 3 & 5.
5+
6+
N = int(input())
7+
even = 0
8+
odd = 0
9+
n = N
10+
while(n != 0):
11+
r = n % 10
12+
if(r % 2 == 0):
13+
even = even+r
14+
else:
15+
odd = odd+r
16+
n = n//10
17+
print(even, odd)
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Given an integer n, find and print the sum of numbers from 1 to n.
2+
# Note: Use while loop only.
3+
4+
n = int(input())
5+
sum = 0
6+
while(n != 0):
7+
sum = sum+n
8+
n = n-1
9+
print(sum)

Diff for: Milestone 1/Introduction/ArithmeticProgression.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# You are given first three entries of an arithmetic progression.
2+
# You have to calculate the common difference and print it.
3+
4+
a = int(input())
5+
b = int(input())
6+
c = int(input())
7+
8+
e = c-b
9+
print(e)

Diff for: Milestone 1/Introduction/FindXraisedtopowerN.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# You are given two integers: X and N. You have to calculate X raised to power N and print it.
2+
3+
X = int(input())
4+
N = int(input())
5+
6+
a = X**N
7+
print(a)

Diff for: Milestone 1/Introduction/FindaverageMarks.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Write a program to input marks of three tests of a student (all integers).
2+
# Then calculate and print the average of all test marks.
3+
4+
a=int(input())
5+
b=int(input())
6+
c=int(input())
7+
d=(a+b+c)/3
8+
print(d)

Diff for: Milestone 1/Introduction/RectangularArea.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# You are given a rectangle in a plane. The coordinates of one of its diagonals are provided to you.
2+
# You have to print the total area of the rectangle.
3+
# The coordinates of the rectangle are provided as four integral values: x1, y1, x2, y2.
4+
# It is given that x1 < x2 and y1 < y2.
5+
6+
x1 = int(input())
7+
y1 = int(input())
8+
x2 = int(input())
9+
y2 = int(input())
10+
11+
ar = (x1-y1)*(x2-y2)
12+
print(ar)

Diff for: Milestone 1/More on Loops/Binary Pattern.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Print the following pattern for the given number of rows.
2+
# Pattern for N = 4
3+
# 1111
4+
# 000
5+
# 11
6+
# 0
7+
8+
n = int(input())
9+
m = n
10+
for i in range(n):
11+
if i % 2 == 0:
12+
print(m*'1', end='')
13+
else:
14+
print(m*'0', end='')
15+
m -= 1
16+
print()

Diff for: Milestone 1/More on Loops/DiamondofStars.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Print the following pattern for the given number of rows.
2+
# Note: N is always odd.
3+
# Pattern for N = 5
4+
# *
5+
# ***
6+
# *****
7+
# ***
8+
# *
9+
10+
n = int(input())
11+
i = 1
12+
s = (n+1)/2
13+
while i <= s:
14+
sp = 1
15+
while sp <= s-i:
16+
print(' ', end='')
17+
sp = sp+1
18+
j = 1
19+
while j <= 2*i-1:
20+
print('*', end='')
21+
j = j+1
22+
print()
23+
i = i+1
24+
i = s-1
25+
m = i
26+
while i >= 1:
27+
j = 1
28+
while j <= m-i+1:
29+
print(' ', end='')
30+
j = j+1
31+
k = 1
32+
while k <= 2*i-1:
33+
print("*", end='')
34+
k = k+1
35+
print()
36+
i = i-1

Diff for: Milestone 1/More on Loops/Print Number Pyramid.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Print the following pattern for a given n.
2+
# For eg. N = 6
3+
4+
# 123456
5+
# 23456
6+
# 3456
7+
# 456
8+
# 56
9+
# 6
10+
# 56
11+
# 456
12+
# 3456
13+
# 23456
14+
# 123456
15+
16+
n = int(input())
17+
i = 1
18+
a = 1
19+
while i <= n:
20+
sp = 1
21+
while sp < i:
22+
print(' ', end='')
23+
sp = sp+1
24+
for j in range(a, n+1):
25+
print(j, end='')
26+
print()
27+
a = a+1
28+
i = i+1
29+
j = j+1
30+
i = 1
31+
a = n-1
32+
while i <= n-1:
33+
sp = 1
34+
while sp <= n-i-1:
35+
print(' ', end='')
36+
sp = sp+1
37+
for j in range(a, n+1):
38+
print(j, end='')
39+
print()
40+
i = i+1
41+
a = a-1

Diff for: Milestone 1/More on Loops/Printthepattern.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Print the following pattern for the given number of rows.
2+
# Pattern for N = 5
3+
# 1 2 3 4 5
4+
# 11 12 13 14 15
5+
# 21 22 23 24 25
6+
# 16 17 18 19 20
7+
# 6 7 8 9 10
8+
9+
def pattern(n):
10+
mid = (n >> 1)
11+
if n & 1 != 0:
12+
mid += 1
13+
val = 1
14+
for i in range(mid):
15+
for j in range(val, val + n):
16+
print(j, end=' ')
17+
print()
18+
val += (n << 1)
19+
if n & 1 != 0:
20+
val -= (n << 1)
21+
val -= n
22+
for i in range(mid, n):
23+
for j in range(val, val + n):
24+
print(j, end=' ')
25+
print()
26+
val -= (n << 1)
27+
else:
28+
val -= n
29+
for i in range(mid, n):
30+
for j in range(val, val + n):
31+
print(j, end=' ')
32+
print()
33+
val -= (n << 1)
34+
35+
36+
n = int(input())
37+
pattern(n)

Diff for: Milestone 1/More on Loops/RectangularNumbers.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Print the following pattern for the given number of rows.
2+
# Pattern for N = 4
3+
# 4444444
4+
# 4333334
5+
# 4322234
6+
# 4321234
7+
# 4322234
8+
# 4333334
9+
# 4444444
10+
11+
MAX = 100
12+
13+
14+
def prints(a, size):
15+
for i in range(size):
16+
for j in range(size):
17+
print(a[i][j], end='')
18+
print()
19+
20+
21+
def innerPattern(n):
22+
23+
size = 2 * n - 1
24+
front = 0
25+
back = size - 1
26+
a = [[0 for i in range(MAX)]
27+
for i in range(MAX)]
28+
while (n != 0):
29+
for i in range(front, back + 1):
30+
for j in range(front, back + 1):
31+
if (i == front or i == back or
32+
j == front or j == back):
33+
a[i][j] = n
34+
front += 1
35+
back -= 1
36+
n -= 1
37+
prints(a, size)
38+
39+
40+
n = int(input())
41+
innerPattern(n)

0 commit comments

Comments
 (0)