Skip to content

Commit 72081d8

Browse files
committed
300 exercise 1.numbers and squares
1 parent 3081ea0 commit 72081d8

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# method 1
2+
print('Numbers\t\tSquares')
3+
print('1 \t\t '+str(1*1))
4+
print('2 \t\t '+str(2*2))
5+
print('3 \t\t '+str(3*3))
6+
print('4 \t\t '+str(4*4))
7+
8+
9+
# method 2
10+
def number_squares(n):
11+
print(str(n)+' \t\t '+str(n*n))
12+
13+
start_num,end_num = input('enter the starting and ending number:').split(' ')
14+
if start_num<=end_num:
15+
for n in range(int(start_num),int(end_num)+1):
16+
number_squares(n)
17+
else:
18+
print('enter the proper range')
19+
20+
21+
# method 3 : optimized
22+
23+
def number_squares(start_num,end_num):
24+
if start_num>end_num:
25+
print('Invalid Range: Starting number must be less than or equal to the ending number.')
26+
return
27+
for n in range(start_num,end_num+1):
28+
print(f"{n}\t\t{n*n}")
29+
30+
try:
31+
start_num,end_num=map(int,input('Enter the starring and ending range with single space:').split(' '))
32+
print('Numbers\t\tSquares')
33+
number_squares(start_num,end_num)
34+
except ValueError:
35+
print("Invalid input! Please enter two integers separated by a space.")

0 commit comments

Comments
 (0)