0% found this document useful (0 votes)
7 views

FINAL pythonLab assignment 4

Python assignment for 3rd sem

Uploaded by

rajmaiti00
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

FINAL pythonLab assignment 4

Python assignment for 3rd sem

Uploaded by

rajmaiti00
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

IT Workshop Lab (Python)

(PCC-CS393)
CSE –3rdSem. –2ndYear

14. Write a Program in Python to generate the following patterns up to Specified


number of Rows:
a) *
* *
***
** **
Program

n=int(input("enter the no of lines:"))

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

n=int(input("enter the number of lines:"))

for i in range(n):
print(" "*i,end="")

for j in range(n-i):
print(" *",end =" ")
print()

Assignment No. – IV Page -1 SIRSHENDU MAITI, 23/CSE/116, </>CSE, FIEM


IT Workshop Lab (Python)
(PCC-CS393)
CSE –3rdSem. –2ndYear

Output
In[1]:
enter the number of lines:4
* * * *
* * *
* *
*
(c). 1
2 2
3 3 3
4 4 4 4

program

n=int(input("enter the no of lines:"))

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

n=int(input("enter the number of rows:"))


num=1
for i in range(1,n+1):
for j in range(1,i+1):
print(num, " ",end=" ")
num=num+1
print("\n")

Assignment No. – IV Page -2 SIRSHENDU MAITI, 23/CSE/116, </>CSE, FIEM


IT Workshop Lab (Python)
(PCC-CS393)
CSE –3rdSem. –2ndYear

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

rows = int(input("enter the no of rows:"))


row = [0] * rows

for i in range(rows):
row[0] = 1

for k in range(rows - i - 1):


print(" ", end="")

print(row[0], end="")

for j in range(1, i + 1):


row[j] = row[j - 1] * (i - j + 1) // j
print(" ", row[j], 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

Assignment No. – IV Page -3 SIRSHENDU MAITI, 23/CSE/116, </>CSE, FIEM


IT Workshop Lab (Python)
(PCC-CS393)
CSE –3rdSem. –2ndYear

16. Write a Program in Python to check whether a


number is Prime or not.

program

n=int(input("enter the number : "))


flag=0

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

Assignment No. – IV Page -4 SIRSHENDU MAITI, 23/CSE/116, </>CSE, FIEM


IT Workshop Lab (Python)
(PCC-CS393)
CSE –3rdSem. –2ndYear

17. Write a Program in Python to find the


sum of all the Prime numbers between a given range of
numbers.

Program

import math

n =int(input("enter the starting number of the range:"))


m = int(input("enter the ending number of the range:"))
l=n

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

sum of prime number between 1 and 101 = 1161

Assignment No. – IV Page -5 SIRSHENDU MAITI, 23/CSE/116, </>CSE, FIEM


IT Workshop Lab (Python)
(PCC-CS393)
CSE –3rdSem. –2ndYear

18. Write a Program in Python to find the sum of all the


Even and Odd numbers between 1 and 100.

Program

start = 1
end = 100

sum_of_odd = 0
sum_of_even = 0

print("Even numbers between 1 and 100 are:", end=" ")


for i in range(start, end + 1):
if i % 2 == 0:
print(f"{i},", end=" ")
sum_of_even += i

print("\b\b ")

print("\nOdd numbers between 1 and 100 are:", end=" ")


for i in range(start, end + 1):
if i % 2 != 0:
print(f"{i},", end=" ")
sum_of_odd += i

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

Assignment No. – IV Page -6 SIRSHENDU MAITI, 23/CSE/116, </>CSE, FIEM


IT Workshop Lab (Python)
(PCC-CS393)
CSE –3rdSem. –2ndYear

19. Write a Program in Python to find & Print the Sum of all the
Numbers Divisible by 7 within a given Range.
program

start = int(input("Enter the start of the range: "))


end = int(input("Enter the end of the range: "))

sum_of_numbers = 0

print("\nthe numbers which are divisible by 7 in the gievn range : ",end="")


for i in range(start, end + 1):
if i % 7 == 0:
print(f"{i},",end=" ")
sum_of_numbers += i

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

The sum of all numbers divisible by 7 between 1 and 50 is = 196

Assignment No. – IV Page -7 SIRSHENDU MAITI, 23/CSE/116, </>CSE, FIEM

You might also like