Computer Python
Computer Python
Computer Python
Output
Output
Generate the following patterns using nested loop.
Pattern-1 Pattern-2 Pattern-3
* 12345 A
** 1234 AB
*** 123 ABC
**** 12 ABCD
***** 1 ABCDE
Code
Pattern 1
for i in range(1,6):
for j in range(i):
print('*',end='')
print('')
Pattern 2
for i in range(5,0,-1):
for j in range(1,i+1):
print(j,end=' ')
print()
Pattern 3-i
n=6
for i in range(n) :
t = 65
for j in range(i + 1) :
print(chr(t), end = ' ')
t += 1
print()
Output
1-x+x^2-x^3+x^4-…x^n
Code
print("Following series: 1-x+x^2-x^3+x^4-…x^n")
x=int(input("Enter value of x: "))
n=int(input("Enter value of n: "))
s=0
j=1
for i in range (n+1):
j=(-1)**i
k=j*(x**i)
s=s+k
print("Sum of the series: 1-x+x^2-x^3+x^4-…x^n")
print("with x:",x,"and n:",n,"is=",s)
Output
x+x^2/2-x^3/3+x^4/4-…x^n/n
Code
n=int(input("Enter the number of terms:"))
x=int(input("Enter the value of x:"))
sum1=1
for i in range(2,n+1):
sum1=sum1+((x**i)/i)
print("The sum of series is",round(sum1,2))
Output
x+x^2/2!-x^3/3!+x^4/4!-…x^n/n!
Code
x = int(input("Enter the value of x: "))
sum = 0
m=1
for i in range(1, 7) :
fact = 1
for j in range(1, i+1) :
fact *= j
term = x ** i / fact
sum += term * m
m = m * -1
print("Sum =", sum)
Output
Output
Input a number and check if the number is a prime or
composite number.
Code
num = int(input("Enter any number : "))
if num > 1:
for i in range(2, num):
if (num % i) == 0:
print(num, "is NOT a PRIME number, it is a COMPOSITE
number")
break
else:
print(num, "is a PRIME number")
elif num == 0 or 1:
print(num, "is a neither Prime NOR Composite number")
else:
print()
Output
Output
Compute the greatest common divisor and the least
common multiple of 2 integers.
Code
num1=float(input("Enter the value of num1: "))
num2=float(input("Enter the value of num2: "))
a=num1
b=num2
lcm=0
while(num2 !=0):
temp=num2
num2=num1%num2
num1=temp
gcd=num1
lcm=((a*b)/gcd)
print("HCF of",a,"and",b,"=",gcd)
print("LCM of",a,"and",b,"=",lcm)
Output
Output
Find the largest/smallest number in a list.
Code
lst = []
num = int(input('How many numbers: '))
for n in range(num):
numbers = int(input('Enter number '))
lst.append(numbers)
print("Maximum element in the list is :", max(lst), "\nMinimum element
in the list is :", min(lst))
Output
Output
Output
Output
Create a dictionary with the roll number, name and
marks of n students in a class and display the name of
students who have marks above 75.
Code
no_of_std = int(input("Enter number of students: "))
result = {}
for i in range(no_of_std):
print("Enter Details of student No.", i+1)
roll_no = int(input("Roll No: "))
std_name = input("Student Name: ")
marks = int(input("Marks: "))
result[roll_no] = [std_name, marks]
print(result)
# Display names of students who have got marks more than 75
for student in result:
if result[student][1] > 75:
print("Student's name who get more than 75 marks is/are",
(result[student][0]))
Output