ASSIGNMENT- 5
NAME- POORVA BUNDEL
ID- 2023UEE1293
QUES 1(a):
a= 5
row=1
col=1
for row in range(1,6):
for col in range(1, row+1):
print(row,end=" ")
print()
OUTPUT:
Ques 1(b):
a=5
b=6
c=7
s=(a+b+c)/2
area=(s*(s-a)*(s-b)*(s-c))**0.5
print("area of given triangle is = ", area )
OUTPUT:
area of given triangle is = 14.696938456699069
QUES 3(A):
n=int(input("enter a number "))
i=1
sum=0
for i in range(1,n+1):
sum=sum+i
print(" sum of the natural number is ", sum)
OUTPUT:
METHOD 2:
n=int(input("enter a number "))
s=(n*(n+1))/2
print("sum of the desired natural numbers is ",s)
OUTPUT:
QUES 3(B):
a=int(input("enter a number"))
b=int(input("enter another number"))
product=a*b
if product>1000:
sum=a+b
print("sum is ", sum)
else:
print("product is ", product)
OUTPUT:
QUES 2(a):
a=int(input("enter a number"))
num1=0
num2=1
print(num1,num2,end=" ")
for i in range(a-2):
num1,num2=num2,(num1+num2)
print(num2,end=" ")
OUTPUT:
QUES 2(C):
N1=int(input("enter the initial number"))
N2=int(input("enter the final number"))
for i in range(N1,N2+1):
if i>0:
is_prime=True
for j in range(2,int(i**0.5)+1):
if i%j==0:
is_prime=False
break
if is_prime:
print(i)
OUTPUT:
QUES 2 ( B ):
num1=int(input("enter a number"))
num2=int(input("enter another number"))
for num in range(num1,num2 +1):
l=len(str(abs(num)))
for digit in str(num):
sum_of_powers = sum(int(digit) ** l for digit in str(num))
print(sum_of_powers)
OUTPUT:
QUES 4(A):
year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(f"{year} is a Leap Year")
else:
print(f"{year} is not a Leap Year")
OUTPUT:
QUES 4(B):
main_string = input("Enter the main string: ")
sub_string = input("Enter the sub-string to search for: ")
count = main_string.count(sub_string)
print(f"The sub-string '{sub_string}' appears {count} times in the given string.")
OUTPUT:
QUES 5(A):
def factorial(num):
if num==0 or num==1:
return 1
else:
return num*factorial(num-1)
num= int(input("enter the number whose you want factorial: "))
if num<0:
print("factorial doesn't exist")
else:
result=factorial(num)
print(f"factorial of {num} is {result}" )
OUTPUT:
QUES 5(B):
dict1={"Poorva":19, "siddharth":14, "Jagriti": 22, "varnika":10}
for name, age in dict1.items():
if age>18:
print(name)
OUTPUT:
QUES 6(A):
import os
f=open(os.path.join("text1"),"r")
length=len(f.read().split(" "))
f.close()
print(f"number of words in text file are: {length}")
QUES 6(B):
f2=open(os.path.join("text2"),"r")
numfile=open("myfile.txt","w")
scores=[]
for x in f2:
scores.append(int(x.split(" ")[1]))
numfile.write(str(sum(scores)/len(scores)))
f2.close()
numfile.close()
QUES 6(C):
try:
f3=open("mynewfile.txt","r")
print(f3.read())
f3.close()
except Exception as e:
print("Don't mess with me.")
print(e)
OUTPUT:
QUES 7(A):
class Student:
def __init__(self, name, percentage):
self.name = name
self.percentage = percentage
def update_percentage(self, new_percentage):
self.percentage = new_percentage
print(f"Updated percentage for {self.name}: {self.percentage}%")
def is_passed(self):
if self.percentage >= 50:
return True
else:
return False
student_name = input("Enter the student's name: ")
student_percentage = float(input(f"Enter {student_name}'s percentage: "))
student = Student(student_name, student_percentage)
if student.is_passed():
print(f"{student.name} has passed!")
else:
print(f"{student.name} has failed!")
new_percentage = float(input(f"Enter the updated percentage for {student.name}: "))
student.update_percentage(new_percentage)
if student.is_passed():
print(f"{student.name} has passed!")
else:
print(f"{student.name} has failed!")
OUTPUT:
QUES 7(B):
import math
def calculate_compound_interest(principal, rate, time, n):
amount = principal * math.pow(1 + (rate / n), n * time)
compound_interest = amount - principal
return compound_interest
principal = float(input("Enter the principal amount: "))
rate = float(input("Enter the annual interest rate (in percentage): ")) / 100
time = float(input("Enter the time period (in years): "))
n = int(input("Enter the number of times interest is compounded per year: "))
compound_interest = calculate_compound_interest(principal, rate, time, n)
print(f"The compound interest is: {compound_interest:.2f}")
OUTPUT:
QUES 8(A):
from matplotlib import pyplot as plt
import numpy as np
X=np.arange(0,2*np.pi,0.1)
Y=np.sin(X)
plt.plot(X,Y)
plt.show()
OUTPUT:
QUES 8(B):
import random
target_number = random.randint(1, 10)
guess = int(input("Enter your guess: "))
if guess < target_number:
print("Too low! Try again.")
elif guess > target_number:
print("Too high! Try again.")
else:
print(f"Congratulations! You've guessed the number {target_number} correctly!.")
OUTPUT:
QUES 9:
def merge_sorted_lists(list1, list2):
return sorted(list1 + list2)
list1 = [1, 3, 5]
list2 = [2, 4, 6]
merged_list = merge_sorted_lists(list1, list2)
print(merged_list)
OUTPUT:
QUES 10:
def merge_sorted_lists(l1,l2):
return sorted(l1+l2)
print(merge_sorted_lists([1,2,5],[0,3,8]))
def flatten(l):
newlist=[]
for i in range(len(l)):
if type(l[i])!=list:
newlist.append(l[i])
else:
l1=flatten(l[i])
newlist.extend(l1)
return newlist
print(flatten([1,[2,[3,4],5],6]))
OUTPUT: