[go: up one dir, main page]

0% found this document useful (0 votes)
11 views19 pages

PRACTICAL FILE

The document contains a series of 20 programming exercises with solutions in Python. Each program demonstrates a specific functionality, such as displaying messages, comparing numbers, calculating powers, checking for palindromes, Armstrong numbers, prime numbers, and more. The solutions include user input and various control structures like loops and conditionals.

Uploaded by

sonaliasutkar85
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views19 pages

PRACTICAL FILE

The document contains a series of 20 programming exercises with solutions in Python. Each program demonstrates a specific functionality, such as displaying messages, comparing numbers, calculating powers, checking for palindromes, Armstrong numbers, prime numbers, and more. The solutions include user input and various control structures like loops and conditionals.

Uploaded by

sonaliasutkar85
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Program.1: Input a welcome message and display it.

Sol:
str=input("Enter a Welcome message")
print(str)

Program 2: Input two numbers and display the


larger / smaller number.
Sol:

n1=int(input("Enter first Number"))


n2=int(input("Enter Second Number"))
if n1>n2:
print("Larger is:",n1)
else:
print("Larger is:",n2)

1
Program 3: Input three numbers and display the
largest number.
Sol:
n1=int(input("Enter first Number"))
n2=int(input("Enter Second Number"))
n3=int(input("Enter Third Number"))

if n1>n2:
if n1>n3:
print("Largest is:",n1)
else:
print("Largest is:",n3)
else:
if n2>n3:
print("Largest is:",n2)
else:
print("Largest is:",n3)

2
Program 4: Given two integers x and n, compute 𝑥n.
Sol:
x=int(input("Enter value for x"))
n=int(input("Enter value for n"))
val=1
i=1
while i<=n:
val=val*x
i=i+1

print("n raise to the power of x=", val)

3
Program 5: Write a program to input the value of x
and n and print the sum of the following series:
S=1+x+x2+x3+x4+x5+……..xn

Sol: x=int(input("Enter value for x"))


n=int(input("Enter value for n"))
s=1
i=1
while i<=n:
s=s+x**i
i=i+1
print("The sum of the series is:",s)

4
Program 6: Write a program to input the value of x
and n and print the sum of the following series:
S=1-x+x2-x3+x4-x5+……..xn

sol:

x=int(input("Enter value for x"))


n=int(input("Enter value for n"))
s=1
i=1
while i<=n:
if i%2==0:
s=s+x**i
else:
s=s-x**i

i=i+1
print("The sum of the series is:",s)

5
Program 7:Write a program to enter a number and
print whether it is Palindrome Number or not?

Sol:

n=int(input("Enter a Number"))
m=n
s=0
while n>0:
r=n%10
s=s*10+r
n=n//10

if s==m:
print("Palindrome")
else:
print("Not Palindrome")

6
Program 8: Write a program to enter a number and
print whether it is Armstrong Number or not?

Sol:
n=int(input("Enter a Number"))
m=n
s=0
while n>0:
r=n%10
s=s+r*r*r
n=n//10

if s==m:
print("Armstrong")
else:
print("Not Armstrong")

7
Program 9: Write a program to enter a number and
print whether it is Prime Number or not?

Sol:
n=int(input("Enter a Number"))
i=2
while i<n:
if n%i==0:
break
i=i+1
if i==n:
print("Prime")
else:
print("Not Prime")

8
Program 10: Write a program to enter a number and
print whether it is Perfect Number or not?
Sol:

n=int(input("Enter a Number"))
sum=0
for i in range(1,n):
if n%i==0:
sum=sum+i

if sum==n:
print("Perfect")
else:
print("Not Perfect")

9
Program 11: Write a program to print Fibonacci
sequence.
Sol:

nterms=int(input("How many terms?"))


n1,n2=0,1
count=0
while count<nterms:
print(n1)
nth=n1+n2
n1=n2
n2=nth
count+=1

10
Program 12: Write a program to enter a string and
count total vowels and consonants in it.
Sol:

str=input("Enter the string")


vowel=0
con=0
for i in str:
if i=='a' or i=='e' or i=='i' or i=='o' or
i=='u':
vowel=vowel+1
else:
con=con+1

print("Total Vowels are:", vowel)


print("Total Consonents are:", con)

11
Program 13: Write a program to enter a string and
count total lower case, upper case and others in
it.
Sol:

str=input("Enter the string")


lower=0
upper=0
other=0
for i in str:
if i.islower():
lower=lower+1
else:
if i.isupper():
upper=upper+1
else:
other=other+1
print("Total lower are:", lower)
print("Total upper are:", upper)
print("Total others are:", other)

12
Program 14: Write a program to enter a string and
print whether it is Palindrome or not? convert the
case of characters in a string.
Sol:

my_str = input("Enter a string")


my_str = my_str.casefold()#it will consider upper
and lower string same
rev_str=my_str[::-1]

if my_str==rev_str:
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")

13
Program 15: Write a program to create a list and
print the smallest element of the list.
Sol:

List=[]
ch='y'
while ch=='y':
n=int(input("Enter a number"))
List.append(n)
ch=input("Wnat to add more element to the
List?(y/n) ")

small=List[0]
for i in List:
if i<small:
small=i

print("Smallest element of the list is:", small)

14
Program 16: Write a program to create a tuple and
print the smallest element of the tuple.
Sol:

Tuple=()
ch='y'
while ch=='y':
n=int(input("Enter a number"))
Tuple=Tuple+(n,)
ch=input("Wnat to add more element to the
List?(y/n) ")

small=Tuple[0]
for i in Tuple:
if i<small:
small=i

print("Smallest element of the Tuple is:", small)

15
Program 17: Write a program to search a number
from a list.
Sol:
List=[12,13,14,15,16,17]
n=int(input("Enter the number to search from the
list"))

found=False
for i in range(0,len(List)-1,1):
if List[i]==n:
print("Element found at:",i+1)
found=True

if found==False:
print("Element not found")

16
Program 18: Write a program to reverse the content
of the list.
Sol:

nums = [1,2,3,4,5,6,7,8,9]
print(nums)
length = len(nums)

for i in range(0,length//2,1):
t=nums[i]
nums[i]= nums[length-1-i]
nums[length-1-i]=t

print(nums)

17
Program 19: Input a list of numbers and swap
elements at the even location with the elements at
the odd location
Sol:
nums = [1,2,3,4,5,6,7,8,9]
print(nums)
length = len(nums)
for i in range(0,length,4):
if(i+2<length):
nums[i], nums[i+2] = nums[i+2], nums[i]
if(i+3<length):
nums[i+1], nums[i+3] = nums[i+3], nums[i+1]

print(nums)

18
Program 20: Create a dictionary with the roll
number, name and marks of n students in a class
and display the names of students who have marks
above 75.
Sol:
n = int(input("Enter number of students: "))
result = {}

for i in range(0,n,1):
print("Enter Details of student No.", i+1)

rno = int(input("Roll No: "))


name = input("Name: ")
marks = int(input("Marks: "))
result[rno] = [name,marks]

print(result)

for student in result:


if result[student][1] > 75:
print(result[student][0])

19

You might also like