XI_Practical File_CS
XI_Practical File_CS
XI_Practical File_CS
Result
Thus the above program to calculate simple interest is run and verified successfully.
1
Ex:No.2. Program to find the largest among three numbers
Date:
Aim:
To write a Python program to find the largest among three numbers.
Algorithm:
1) Start.
2) Read three integers from the user.
3) Use if-elif statement, to find the largest among three numbers.
4) Print the result.
5) Stop.
Program:
#Program to find largest among three numbers
a=int(input("Enter Number 1:"))
b=int(input("Enter Number 2:"))
c=int(input("Enter Number 3:"))
if a>b and a>c:
print("Largest Number is:",a)
elif b>a and b>c:
print("Largest Number is:",b)
else:
print("Largest Number is:",c)
Output:
Enter Number 1:25
Enter Number 2:63
Enter Number 3:21
Largest Number is: 63
Result:
Thus the above program to find the largest among three numbers is run and verified
successfully.
2
Ex:No.3 Program to check if a Number is a Prime Number
Date:
Aim:
To write a Python program to check if a Number is a Prime Number
Algorithm:
1) Start
2) Read the number to be checked and store it in a variable.
3) Initialize the count variable to 0.
4) Let the for loop range from 2 to half of the number.Find the number of divisors using
if statement and increment the count variable each time.
5) If the number of divisors is equal to 0, the number is prime else not prime.
6) Print the final result.
7) Stop.
Program:
# Prime number or not
a=int(input("Enter number: "))
k=0
for i in range(2,a//2+1):
if(a%i==0):
k=k+1
if(k==0):
print("Number is prime")
else:
print("Number isn't prime")
Output:
Enter number: 25
Number isn't prime
Enter number: 37
Number is prime
Result:
Thus the above program to check whether the given number is prime or not is run and
verified successfully.
Ex:No.4 Program to print all the Prime Numbers within a Given Range
Date:
Aim:
3
To write a Python program to print all the Prime Numbers within a Given Range
Algorithm:
1. Start
2. Get the upper limit for the range and store it in a variable.
3. Let the first for loop range from 2 to the upper limit.
4. Initialize the count variable to 0.
5. Let the second for loop range from 2 to half of the number
6. Then find the number of divisors using if statement and increment the count variable
each time.
7. If the number of divisors is equal to 0, the number is prime.
8. Print the number.
9. Stop
Program:
# List of Prime numbers
Num=int(input("Enter upper limit: "))
for a in range(2,Num+1):
k=0
for i in range(2,a//2+1):
if(a%i==0):
k=k+1
if(k==0):
print(a)
Output:
Enter upper limit: 12
2
3
5
7
11
Result:
Thus the above program to generate prime number within specified limit is run and
verified successfully.
Aim:
To write a Python program to find the sum of digits in a number.
4
Algorithm:
1. Start.
2. Take the value of the integer and store in a variable.
3. Using a while loop, get each digit and add the digits to a variable.
4. Print the sum of the digits of the number.
5. Stop.
Program:
# Sum of digits in a number
n=int(input("Enter a number:"))
tot=0
while(n>0):
dig=n%10
tot=tot+dig
n=n//10
print("The total sum of digits is:", tot)
Output:
Enter a number:153
The total sum of digits is: 9
Result:
Thus the above program to calculate sum of digits in a number is run and verified
successfully
Aim:
To write a Python program to check if a Number is a Palindrome
Algorithm:
1. Start.
2. Take the value of the integer and store in a variable.
3. Transfer the value of the integer into another temporary variable.
4. Using while loop, get each digit of the number and store the reversed number in
another variable.
5. Check if the reverse is equal to the temporary variable, print ‘Palindrome’ else print
‘Not palindrome’
6. Stop.
Program:
5
# Given number is palindrome or not
n=int(input("Enter number:"))
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print("The number is a palindrome!")
else:
print("The number isn't a palindrome!")
Output:
Enter number:424
The number is a palindrome!
Result:
Thus the above program to verify whether the given number is palindrome or not is
run and verified successfully.
Aim:
To write a Python program to find factorial of a number.
Algorithm:
1. Start.
2. Get any integer value from the user and store it in variable.
3. Assign 1 to variable fact and a.
4. Calculate factorial of a number using while loop.
5. Print the result.
6. Stop.
Program:
# Factorial of a number
n=int(input("Enter a number:"))
fact=1
a=1
while a<=n:
fact=fact*a
a=a+1
6
print("The factorial of ",n," is ",fact)
Output:
Enter a number:4
The factorial of 4 is 24
Result:
Thus the above program to find factorial of anumber is run and verified successfully.
Aim:
To write a Python program to generate Fibonacci series
Algorithm:
1. Start
2. Get number of terms to be printed in series and store it in N
3. Assign 0 to First and 1 to Second
4. Print the value of First, Second.
5. Let the for loop range from 1 to N-1.
6. Generate Fibonacci series by adding the value of First and Second and store it in
Third, print the value of Third.
7. Do the above process repeatedly using for loop for N number of times.
8. End.
Program:
# Fibonacci series
N=int(input("Enter number of terms to be printed in Fibonacci series:"))
First=0
Second=1
print(First,Second,end=" ")
for i in range(1,N-1):
Third=First+Second
First=Second
Second=Third
print(Third,end=" ")
Output:
Enter number of terms to be printed in Fibonacci series:6
011235
Result:
7
Thus the above program to generate fibonacci series is run and verified successfully.
Aim:
To write a Python program to print the following patterns using nested for loop.
Algorithm:
1. Start
2. Get number of lines to be printed in pattern and store it in N
3. Use outer for loop to decide number of lines to be printed.
4. Use inner for loop to decide number of characters to be displayed in each line.
5. Print the pattern.
6. End.
Program:
a)
N = int(input("Enter number of rows:"))
for i in range (0, N):
for j in range(0, i + 1):
print("*", end=' ')
print( )
b)
N=int(input("Enter number of lines to be printed:"))
for row in range(1, N+1):
for column in range(1, row + 1):
print(column, end=' ')
print( )
c)
N = int(input("Enter number of rows:"))
for i in range (0, N):
ch=65
for j in range(0, i + 1):
print(chr(ch),end=' ')
8
ch=ch+1
print( )
d)
N = int(input("Enter number of rows:"))
for i in range (0, N):
for j in range(N,i,-1):
print(j, end=' ')
print( )
Output:
Enter number of rows:5
*
**
***
****
*****
Enter number of lines to be printed:5
1
12
123
1234
12345
Enter number of rows:5
A
AB
ABC
ABCD
ABCDE
Enter number of rows:5
54321
5432
543
54
5
Result:
Thus the above programs to print patterns in run and verified successfully.
Ex:No.10 Program to count no. of alphabets, digits, blank spaces and special characters
in Date: a string
Aim:
9
To write a Python program to count no. of alphabets, digits, blank spaces and special
characters in a string.
Algorithm:
1. Start
2. Read string from the user and store it in variable.
3. Declare count variables and assign 0.
4. Check if the string contains alphabets, digits, blank spaces and special characters
using built-in functions and increment count variables accordingly.
5. Print the final result.
6. Stop.
Program:
# Counting characters in a string
string=input("Enter line of text:")
alpha_count=0
digit_count=0
space_count=0
sp_count=0
for a in string:
if a.isalpha():
alpha_count+=1
elif a.isdigit():
digit_count+=1
elif a.isspace():
space_count+=1
else:
sp_count+=1
print("Number of Alphabets:",alpha_count)
print("Number of Digits:",digit_count)
print("Number of Spaces:",space_count)
print("Number of Special characters:",sp_count)
Output:
Enter line of text:India have won the match by 79 runs
Number of Alphabets: 26
Number of Digits: 2
Number of Spaces: 7
Number of Special characters: 0
Result:
Thus the above program to count no. of alphabets, digits, blank spaces and special
characters in a string is run and verified successfully.
10
Ex:No.11 Program to check if a Substring is Present in a Given String
Date:
Aim:
To write a Python program to check if a Substring is Present in a Given String
Algorithm:
1. Start
2. Read string and substring from the user and store it in separate variables.
3. Check if the substring is present in the string using find() function.
4. Print the final result.
5. Stop.
Program:
# Finding substring in given string
string=input("Enter line of text:")
sub_str=input("Enter word to be searched:")
if(string.find(sub_str)==-1):
print("Word not found in string!")
else:
print("Word found in string!")
Output:
Enter line of text:Little Kingdom School
Enter word to be searched:Little
Word found in string!
Result:
Thus the above program to check whether the substring exists in the original string is
run and verified successfully
Ex:No.12 Program to Count the Occurrences of Each Word in a Given String Sentence
Date:
Aim:
To write a Python program to count the occurrences of each word in a given string
sentence.
Algorithm:
1. Start
2. Read string and a word from the user and store it in separate variables.
3. Initialize count variable to 0.
4. Split the string using space as the reference and store the words in a list.
11
5. Use for loop to traverse through the words in the list and use if statement to check if
the word in the list matches the word given by the user and increment the count.
6. Print the total count of the variable.
7. Stop.
Program:
# Counting words in a string
string=input("Enter line of input:")
word=input("Enter word:")
a=[ ]
count=0
a=string.split(" ")
for i in range(0,len(a)):
if(word==a[i]):
count=count+1
print("Count of the word is:")
print(count)
Output:
Enter line of input:Delhi is the Capital of India, Chennai is the Capital of Tamil Nadu
Enter word:Capital
Count of the word is:
2
Result:
Thus the above program to count the occurrences of each word in a given string
sentence is run and verified successfully.
Aim:
To write a Python program to find the smallest / largest Number in a List
Algorithm:
1. Start
2. Read the number of elements to be stored in list.
3. Read the elements of the list one by one and append it in list.
4. Print the smallest / largest element of the list using min() and max().
5. Stop.
Program:
12
# Finding smallest/largest number in list
a=[ ]
n=int(input("Enter number of elements to be stored in List:"))
for i in range(0,n):
b=int(input("Enter element:"))
a.append(b)
print("Smallest element is:",min(a))
print("Largest element is:",max(a))
Output:
Enter number of elements to be stored in List:5
Enter element:20
Enter element:2
Enter element:35
Enter element:6
Enter element:90
Smallest element is: 2
Largest element is: 90
Result:
Thus the above program to find the smallest / largest Number in a List is run and
verified successfully.
Ex:No.14 Program to Print Largest Even and Largest Odd Number in a List
Date:
Aim:
To write a Python program to print largest even and largest odd number in a List.
Algorithm:
1. Start
2. Read number of elements to be stored in list from the user.
3. Read the elements from the user using for loop and append to a list.
4. Using for loop, get the elements one by one from the list and check if it is odd or
even and append them to different lists.
5. Sort both the lists individually and reverse it.
6. Print the largest odd element and largest even element from the sorted lists.
7. Stop.
Program:
# Finding largest odd / largest even number in list
n=int(input("Enter the number of elements to be in the list:"))
13
b=[ ]
for i in range(0,n):
a=int(input("Element: "))
b.append(a)
c=[ ]
d=[ ]
for i in b:
if(i%2==0):
c.append(i)
else:
d.append(i)
c.sort(reverse=True)
d.sort(reverse=True)
print("Largest even number:",c[0])
print("Largest odd number",d[0])
Output:
Enter the number of elements to be in the list:5
Element: 90
Element: 21
Element: 10
Element: 78
Element: 22
Largest even number: 90
Largest odd number 21
Result:
Thus the above program to largest even and largest odd number in a List is run and
verified successfully.
Aim:
To write a Python program to modify the list elements which are divisible by 10 as 0.
Algorithm:
1. Start
2. Read number of elements to be stored in list from the user.
3. Read the elements from the user using for loop and append to a list.
4. Using for loop, get the elements one by one from the list and change it as ‘0’ if it is
divisible by 10.
5. Print the Modified list.
14
6. Stop.
Program:
# Modification of list elements
N=int(input("Enter no. of elements to be stored in List:"))
Lst=[ ]
for i in range(0,N):
a=int(input("Enter element:"))
Lst.append(a)
print("Original List:",Lst)
for i in range(0,len(Lst)):
if Lst[i]%10==0:
Lst[i]=0
print("Modified List:",Lst)
Output:
Enter no. of elements to be stored in List:5
Enter element:21
Enter element:98
Enter element:30
Enter element:45
Enter element:25
Original List: [21, 98, 30, 45, 25]
Modified List: [21, 98, 0, 45, 25]
Result:
Thus the above program to modify the list elements which are divisible by 10 as 0 is
run and verified successfully.
Aim:
To write a Python program to find sum of all the values which are ending with 3.
Algorithm:
1. Start
2. Create list of integers.
3. Using for loop, get the elements one by one from the list and check whether the
element is ending with 3.
4. Update sum variable.
5. Print sum.
6. Stop.
15
Program:
# Sum of elements which are ending with 3
L=[10,3,56,73,21,33,50]
print("The List elements are:",L)
a=len(L)
sum=0
for i in range(a):
if type(L[i])==int:
if L[i]%10==3:
sum=sum+L[i]
print("Sum of elements which are ending with 3:",sum)
Output:
The List elements are: [10, 3, 56, 73, 21, 33, 50]
Sum of elements which are ending with 3: 109
Result:
Thus the program to find sum of all the values which are ending with 3 is run and
verified successfully.
Aim:
To write a Python program to search an element in a list.
Algorithm:
1. Start
2. Read list of elements in ascending order from the user.
3. Read the element to be searched from the user.
4. Traverse the list using for loop, compare the element to be searched with the list
elements.
5. If it is equal then print its position else print “Not Found”.
6. Stop.
Program:
#Program to search an element in a list
a=eval(input("Enter list values in ascending order:"))
b=int(input("Enter the number to be searched:"))
L=len(a)
found=0
for i in range(L):
16
if a[i]==b:
found=1
print("Your search is Success!")
print("Element found at position ",i+1)
break
if found==0:
print("Element Not Found!")
Output:
Enter list values in ascending order:[21,45,67,89,120]
Enter the number to be searched:67
Your search is Success!
Element found at position 3
Result:
Thus the above program to search an element in a list is run and verified successfully.
Aim:
To write a Python program to create a tuple and find minimum and maximum
element from the tuple.
Algorithm:
1. Start
2. Read number of elements to be stored in tuple from the user.
3. Read the elements from the user using for loop and add it to the tuple.
4. Find the minimum and maximum value from the tuple using min() and max()
5. Print the result.
6. Stop.
Program:
# Program to create tuple
T=tuple()
n=int(input("Enter number of elements to be in the tuple:"))
for i in range(n):
a=int(input("Enter number:"))
17
T=T+(a,)
print("Tuple Elements are:",T)
print("Maximum element in the tuple:",max(T))
print("Minimum element in the tuple:",min(T))
Output:
Enter number of elements to be in the tuple:5
Enter number:25
Enter number:63
Enter number:54
Enter number:89
Enter number:25
Tuple Elements are: (25, 63, 54, 89, 25)
Maximum element in the tuple: 89
Minimum element in the tuple: 25
Result:
Thus the above program to create a tuple and find minimum and maximum element
from the tuple is run and verified successfully.
Aim:
To write a Python program to create a dictionary.
Algorithm:
1. Start
2. Read number of elements to be stored in dictionary from the user.
3. Read key element and value element from the user and store it in dictionary.
4. Display the content of dictionary.
5. Stop.
Program:
# Program to create and store product details in dictionary.
D={}
n=int(input("Enter number of products to be stored:"))
for i in range(n):
p=input("Enter Product name:")
q=int(input("Enter Quantity:"))
D[p]=q
print("\nProduct Details")
18
print("----------------------")
a=1
for i in D.keys():
print(a,".",i,":",D[i])
a=a+1
Output:
Enter number of products to be stored:3
Enter Product name:Pen
Enter Quantity:5
Enter Product name:Pencil
Enter Quantity:7
Enter Product name:Eraser
Enter Quantity:10
Product Details
----------------------
1 . Pen : 5
2 . Pencil : 7
3 . Eraser : 10
Result:
Thus the above program to create a product dictionary is run and verified
successfully.
Aim:
To write a Python program to create a dictionary and search data from it.
Algorithm:
1. Start
2. Read number of elements to be stored in dictionary from the user.
3. Read key element and value element from the user and store it in dictionary.
4. Read the key element to be searched from the user and traverse the for loop to find
the corresponding value element.
5. Print the result.
6. Stop.
Program:
# Program to create and search data in dictionary
19
d=dict()
n=int(input("Enter number of states:"))
for i in range(n):
s=input("Enter State:")
c=input("Enter Capital:")
d[s]=c
k=d.keys()
print("\nSTATE:","CAPITAL")
for i in k:
print(i,':',d[i])
print()
s=input("Enter State name to know its Capital:")
for i in d.keys():
if i==s:
print(d[i])
break;
else:
print("Not found")
Output:
Enter number of states:2
Enter State:Tamil Nadu
Enter Capital:Chennai
Enter State:Kerala
Enter Capital:Trivandrum
STATE: CAPITAL
Tamil Nadu : Chennai
Kerala : Trivandrum
Result:
Thus the above program to create a dictionary and search data from it is run and
verified successfully.
20