FINAL pythonLab assignment 4
FINAL pythonLab assignment 4
(PCC-CS393)
CSE –3rdSem. –2ndYear
for i in range(1,n+1):
for k in range(i,n):
print(" ",end="")
for j in range (1,i+1):
print("*",end=" ")
print("")
OUTPUT
In [1]:
enter the no of lines:4
*
* *
***
** **
(b) * * * *
* * *
* *
*
Program
for i in range(n):
print(" "*i,end="")
for j in range(n-i):
print(" *",end =" ")
print()
Output
In[1]:
enter the number of lines:4
* * * *
* * *
* *
*
(c). 1
2 2
3 3 3
4 4 4 4
program
for i in range(1,n+1):
for k in range(i,15+n):
print(" ",end="")
for j in range (1,i+1):
print(f"{i}",end=" ")
print("")
output
In [1]:
enter the no of lines:4
1
22
333
4444
(d). 1
2 3
4 5 6
7 8 9 10
Program
output
In [1]:
enter the number of rows:4
1
2 3
4 5 6
7 8 9 10
15. Write a Program in Python to generate the PASCAL’s triangle up to specified number of rows.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
program
for i in range(rows):
row[0] = 1
print(row[0], end="")
print()
output
In [1]:
enter the no of rows:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
program
for i in range(2,n):
if n<=1:
print(f"{n} is not a prime numbr")
elif n%i==0:
flag=1
break
if flag==0:
print(f"{n} is a prime number")
else:
print(f"{n} is not a prime number")
Output
In [1]:
enter the number : 17
17 is a prime number
In [2]:
enter the number : 65
65 is not a prime number
Program
import math
sum =0
print("\t\t\n\nprime number in the given range is : ",end=" ")
for n in range(1,m+1):
flag=0
if(n==0 or n==1):
continue
for i in range(2,int(math.sqrt(n))+1):
if (n%i ==0):
flag=1
if flag==0:
print(f"{n},",end=" ")
sum+=n
print("\b\b ")
print(f"\nsum of prime number between {l} and {m} = {sum}")
output
In [1]:
enter the starting number of the range:1
enter the ending number of the range:101
prime number in the given range is : 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61,
67, 71, 73, 79, 83, 89, 97, 101
Program
start = 1
end = 100
sum_of_odd = 0
sum_of_even = 0
print("\b\b ")
print("\b\b ")
print(f"\nThe sum of all even numbers between {start} and {end} is: {sum_of_even}")
print(f"The sum of all odd numbers between {start} and {end} is: {sum_of_odd}")
output
In [1]:
Even numbers between 1 and 100 are: 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44,
46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100
Odd numbers between 1 and 100 are: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43,
45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99
The sum of all even numbers between 1 and 100 is: 2550
The sum of all odd numbers between 1 and 100 is: 2500
19. Write a Program in Python to find & Print the Sum of all the
Numbers Divisible by 7 within a given Range.
program
sum_of_numbers = 0
print("\b\b ")
print(f"\nThe sum of all numbers divisible by 7 between {start} and {end} is = {sum_of_numbers}")
output
In [1]:
Enter the start of the range: 1
Enter the end of the range: 50
the numbers which are divisible by 7 in the gievn range : 7, 14, 21, 28, 35, 42, 49