0% found this document useful (0 votes)
16 views30 pages

Practical of Cs 23

Uploaded by

yatikarajput00
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)
16 views30 pages

Practical of Cs 23

Uploaded by

yatikarajput00
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/ 30

Program:1

#Input a welcome message and display it

message-input("enter a welcome
message:")

print (message)
Output:
enter a welcome messagewelcome to
python world

welcome to python world


Program:2
#Input two numbers and display the
larger / smaller number

x=int(input("enter a number:"))

y-int (input("enter another number:"))

if x>y:

print('larger number:',x)

else:

print('larger number:',y)
Output:
enter a number: 70
enter another number:90

larger number: 90
Program:3
#Input three numbers and display the
largest / smallest number

x=int(input("enter a first number:"))

y=int(input("enter second number:"))

z=int(input("enter a third number:"))

if x>y and x>z:

print ("larger number",x)

elif y>z and y>x:

print ("larger number", у)

else:

print ("larger number",z)


Output:
enter a first number:56

enter second number:98

enter a third number: 78

larger number 98
Program:4(a)
''Generate the following patterns using
nested loops
‘’’Generate the following patterns using
nested loops
*
**
***
****
*****’’’

for i in range (1,6):

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

print("",end='')

for i in range (1,6):

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


print("*",end='')
print ()
Output:
*
**
***
****
Program:4(b)
'''Generate the following patterns using
nested loops

12345
1234
123
12
1'''
n=5

for i in range (n,0,-1):

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

print(j,end='')

print()
Output:
12345
1234
123
12
1

Program :4 (c)
'''Generate the following patterns using
nested loops

A
AB
ABC
ABCD
ABCDE'''

for i in range (65,70,1):

for j in range (65,1+1,1): print (chr(3),end='')

print()

Output:

A
AB
ABC
ABCD
ABCDE
Program:5(a)
'''Write a program to input Write a program
to input the value of x and n and print the
sum of the following series:
1 + + 2+ 3 + x4 + n'''
x=int(input("enter a value for x:"))
n=int(input("enter a value for n:"))
s=0
for i in range(n+1):
k=x**i
s=s+k
print("for x:",x,"for n;", n,"that is equal to:",s)

output:
enter a value for x:2
enter a value for n:5
for x: 2 for n; 5 that is equal to: 63

Program:5(b)
'''Write a program to input the value of x and
n and print the sum of the following series:
1− + 2− 3+ 4− n'''
import math
x=int(input("enter a value for x:"))
n=int(input("enter a value for n:"))
s=0
j=1
for i in range(n+1):
j=math.pow(-1,i)
k=math.pow(x,i)
k=k*j
s+=k
print("for x:",x,"for n",n, "that is equal to:",s)
output:
enter a value for x:2
enter a value for n:5
for x: 2 for n 5 that is equal to: -21.0

Program:5 (c)
'''Write a program to input the value of x and
n and print the sum of the following series
+ 2/2+ 3/3+ 4/4+ /n'''
import math
x=int(input("enter a value for x"))
n=int(input("enter a value for n"))
s=x
j=0
if n==1:
print("for x:",x,"for n",n, "that is equal to:",s)
else:
for i in range(2,n+1):
j=(-1)**i
k=(j)(x*i)/i
k=k*j
s=s+k
print("for x:",x,"for n",n, "that is equal to:",s)
output:
enter a value for x2
enter a value for n8
for x: 2 for n 8 that is equal to:
78.01904761904763
Program:5(d)
‘’’Write a program to input the value of x
and n and print the sum of the following
series: + 2/ 2! + 3/ 3! + 4/ 4! + /
!’’’
x=int(input("enter a value for x"))
n=int(input("enter a value for n"))
s=x
j=0
factorial=1
if n==1:
print("for x:",x,"for n",n, "that is equal to:",s)
else:
for i in range(2,n+1):
factorial=factorial*i
j=(-1)**i
k=(j)(x*i)/i
k=k*j
s=s+k
print("for x:",x,"for n",n, "that is equal to:",s)
output:
enter a value for x3
enter a value for n9
for x: 3 for n 9 that is equal to:
3526.4035714285715

Program:6
number = int(input("Enter a number: "))
divisors_sum = 0
for i in range(1, number):
if number % i == 0:
divisors_sum += i

if divisors_sum == number:
print(f"{number} is a Perfect number.")
else:
print(f"{number} is not a Perfect
number.")
num_str = str(number)
power = len(num_str)
armstrong_sum = 0
for digit in num_str:
armstrong_sum += int(digit) ** power

if armstrong_sum == number:
print(f"{number} is an Armstrong
number.")
else:
print(f"{number} is not an Armstrong
number.")

# Check for Palindrome


if str(number) == str(number)[::-1]:
print(f"{number} is a Palindrome.")
else:
print(f"{number} is not a Palindrome.")
Output>>>
153 is not a Perfect number.
153 is an Armstrong number.
153 is not a Palindrome.
program:7
#Input a number and check if the number
is a prime or composite number.
n=int(input("enter a number:"))
if n>1:
for i in range(2,n):
if n%i==0:
print("number is not prime")
break
else:
print("number is prime")
else:
print("number is not prime")
output:
enter a number:66
number is not prime
program:9
#Compute the greatest common divisor
and least common multiple of two integers
n1=int(input("enter a first value"))
n2=int(input("enter a second value"))
a=n1
b=n2
lcm=0
while n2!=0:
c=n2
n2=n1%n2
n1=c
gcd=n1
lcm=n1*n2/gcd
print("hcf of",a,"and",b, "=",gcd)
print("lcm of",a,"and",b,"=",lcm)
output:
enter a first value78
enter a second value90
hcf of 78 and 90 = 6
lcm of 78 and 90 = 0.0
program:10
#Count and display the number of vowels,
consonants, uppercase, lowercase
characters in
#string.
str=input("enter a string")
vow=0
cons=0
low=0
up=0
for i in range(0,len(str)):
if (str[i].isupper()):
up=up+1
elif (str[i].islower()):
low=low+1
str=str.lower()
for i in range(0,len(str)):
if (str[i]=="a" or str[i]=="e" or str[i]=="i" or
str[i]=="o" or str[i]=="u"):
vow=vow+1
elif (str[i]>="a" and str[i]<="z") :
cons=cons+1
print ("vowels",vow)
print ("consonants",cons)
print ("uppercase",up)
print ("lowercase",low)
Output:
=========================
enter a stringCOMPUTER SCIENCE
students
vowels 8
consonants 15
uppercase 15
lowercase 8
program:11
#Input a string and determine whether it is
a palindrome or not; convert the case of
characters
#in a string.
str=input("Enter a string:")
str =str.casefold()
str1 = reversed(str)
if list(str) == list(str1):
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")
Output:
Enter a string:PHOBIAAIBOHP
The string is a palindrome.
Program:12
# Input string
string = input("Enter a string: ")

# Check for palindrome


is_palindrome = string.lower() ==
string[::-1].lower()

# Convert the case of characters


converted_string = string.swapcase()

# Output results
if is_palindrome:
print(f"The string '{string}' is a
palindrome.")
else:
print(f"The string '{string}' is not a
palindrome.")
print(f"The string with case converted:
'{converted_string}'")

Program:13
'''Input a list of numbers and swap
elements at the even location with the
elements at
the odd location'''
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
for i in range(0, len(numbers) - 1, 2
numbers[i], numbers[i + 1] = numbers[i + 1],
numbers[i]
print(numbers)
For the input list[1,2,3,4,5,6,7,8]

Output>>>
[2, 1, 4, 3, 6, 5, 8, 7]
Program:14
'''Input a list/tuple of elements, search for a
given element in a list'''
# Input list or tuple
elements = [1, 2, 3, 4, 5, 6, 7]

search_element = 4
Program :15
#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

n=int(input("Enter the number of students:


"))

student data

for i in range(n):

roll_number = int(input("Enter roll number


for student: "))

name = input("Enter name for student: ")


marks int (input("Enter marks for student:
"))

student data [roll number] = {'name': name,


'marks': marks)

print (student_data)

print("Students who have secured marks


above 75:")

for roll number, details in student


data.items():

if details['marks'] > 75:

print (details['name'])

Output:
Enter the number of students: 4
Enter roll number for student: 1

Enter name for student: ash

Enter marks for student: 100

Enter roll number for student: 2

Enter name for student: garry

Enter marks for student: 74

Enter roll number for student: 3

Enter name for student: TANJIRO

Enter marks for student: 99

Enter roll number for student: 4

Enter name for student: zenitsu

You might also like