0% found this document useful (0 votes)
24 views28 pages

CLASS 11 LAB PRG-complete

The document outlines 10 Python programs covering topics like input/output, arithmetic operations, pattern generation, series calculation, number checking, prime number checking, Fibonacci series, GCD/LCM calculation, and string analysis.

Uploaded by

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

CLASS 11 LAB PRG-complete

The document outlines 10 Python programs covering topics like input/output, arithmetic operations, pattern generation, series calculation, number checking, prime number checking, Fibonacci series, GCD/LCM calculation, and string analysis.

Uploaded by

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

CLASS 11 COMPUTER SCIENCE (083) LAB PROGRAMS--- Thriveni

///PROGRAMS 1 Python program to input welcome message and print it

//PROGRAM 2 Python program to input two numbers and


display larger and smaller Number
CLASS 11 COMPUTER SCIENCE (083) LAB PROGRAMS--- Thriveni

///PROGRAM3 Python Program to find the Biggest and Smallest


of 3 numbers
CLASS 11 COMPUTER SCIENCE (083) LAB PROGRAMS--- Thriveni

///PROGRAM 4 -- Generate left triangle star pattern using


nested loop

-
//PROGRAM 4 --- Python code for number triangle pattern

///PROGRAM 4 --- Python code for Left Triangle Alphabet pattern using nested for loop
CLASS 11 COMPUTER SCIENCE (083) LAB PROGRAMS--- Thriveni

//PROGRAM 5----Write a program to input the value of x and n


and print the sum of the following series: 1) 1 + x² + x³ + … + xⁿ

//PROGRAM 5 ---- Write a program to input the value of x and n and print
the sum of the following series: 2) 1 -x + x² – x³ + … + xⁿ
CLASS 11 COMPUTER SCIENCE (083) LAB PROGRAMS--- Thriveni

//PROGAM 5 ---3RD SUB QUESTION

//PROGRAM 5 ---4TH SUB QUESTION


CLASS 11 COMPUTER SCIENCE (083) LAB PROGRAMS--- Thriveni

//PROGRAM 6 --- Determine whether a number is a perfect ,an


Armstrong or a palindrome number
CLASS 11 COMPUTER SCIENCE (083) LAB PROGRAMS--- Thriveni
CLASS 11 COMPUTER SCIENCE (083) LAB PROGRAMS--- Thriveni
CLASS 11 COMPUTER SCIENCE (083) LAB PROGRAMS--- Thriveni

///COMBINING ALL PROGRAMS INTO ONE PROGRAM


CLASS 11 COMPUTER SCIENCE (083) LAB PROGRAMS--- Thriveni

//PROGRAM 7 ::Python Program to check if the number is


prime or composite number
What is Prime number?
Any natural number that is divisible by 1 and itself called Prime Number in Python.
Prime number is not divisible by any other numbers except one and itself.

Prime Numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59,
61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109 etc

What is composite number?


Any positive integer that can be formed by multiplying two smaller positive
integers is called composite number. In other word, Composite number is a
positive integer that has at least one divisor other than 1 and itself.

We can say that composite numbers are exactly the numbers that are not prime
and not a unit.

Composite numbers are 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26,
27, 28, 30, 32, 33, 34, 35, 36, 38, 39, 40, 42, 44, 45, 46, 48, 49, 50, 51, 52, 54, 55,
56, 57, 58, 60, 62, 63, 64, 65, 66, 68, 69, 70, 72, 74, 75, 76, 77, 78, 80, 81, 82, 84,
85, 86, 87, 88, 90, 91, 92, 93, 94, 95, 96, 98, 99, 100, 102, 104, 105, 106, 108,
110, 111, 112, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126,
128, 129, 130, 132, 133, 134, 135, 136, 138, 140, 141, 142, 143, 144, 145, 146,
147, 148, 150 etc.

For example:

For example, the integer 4 is a composite number because it is the product of the
two smaller integers 2 X 2 Likewise, the integer 2 is not composite numbers
because it can only be divided by one and itself.

Program Logic:

 Take a any number from user using input method


 Use if-elif statement to check number is zero or 1
 If number is zero or one then given number is neither prime number nor
composite number
 If user entered number is negative number then program ask user to enter
only positive number.
 If number is greater than one then only number can be either prime number
or composite number.
 Use for loop to iterate through number
 Use if statement within for loop to check number is prime number or
composite number
 If given condition is true then number is composite number otherwise prime
number.
CLASS 11 COMPUTER SCIENCE (083) LAB PROGRAMS--- Thriveni

Program Description:
Within the for loop, there is for statement to check whether the Number divisible
by i is exactly equal to 0 or not. If the condition is True, then entered number is
not prime number; it will be composite number and then break statement is
executed. If condition is false,then given number is prime number but not
composite number.

Below is output
>>> %Run 'compositeor prime.py'
Enter any number:3
3 number is prime but not composite number
>>> %Run 'compositeor prime.py'
Enter any number:2
2 number is prime but not composite number
>>> %Run 'compositeor prime.py'
Enter any number:8
8 is not prime but composite number
Below is Snapshot of complete code with output

PROGRAM 8::: Python program to display the terms of Fibonacci


series
Python program to print terms of Fibonacci Series Using for Loop
This Python Fibonacci series program allows the user to enter any positive integer
and displays the terms of Fibonacci series using Python for Loop.
CLASS 11 COMPUTER SCIENCE (083) LAB PROGRAMS--- Thriveni

Fibonacci Series starts 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …

The first two terms are 0 and 1. All other terms are obtained by adding the
preceding two terms.

First term = 0

Second term = 1

Third term = first term + Second term

Third term will be obtained by adding first two terms. Hence; third term will be 0
+ 1 = 1.

Fourth term will be obtained by adding second and third term; Hence fourth term
will be 1 + 1 = 2

Fifth term will be obtained by adding third and fourth term ; Hence Fifth term will
be 1 + 2 = 3

Sixth term will be obtained by adding fourth term and fifth term ; Hence Sixth
term will be 2 + 3 = 5 and so on.

Output:
>>> %Run fibonacci.py
Enter the number of terms : 10
0 1 1 2 3 5 8 13 21 34
>>> %Run fibonacci.py
Enter the number of terms : 20
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
Program Description
In this program , we declared two integer variables n1 and n2 as first two terms
and assign 0 and 1 respectively.This Python Fibonacci series program allows the
user to enter any positive integer and then, that number assigned to variable
Number.

We used if statement to check whether user had entered positive number or not. If
user had entered positive number then it will print first two terms and then we
used for loop which start from 2 to user specified number. Next, new term will be
obtained by adding preceding two term
CLASS 11 COMPUTER SCIENCE (083) LAB PROGRAMS--- Thriveni

Below is Snapshot of Fibonacci Series with output

PROGRAM 9::::Write a Python program to Compute the greatest


common divisor (GCD) and least common multiple (LCM) of two
integer

This python program allows the user to enter two positive integer values and
compute GCD using while loop. Next, python program calculate LCM of two
positive integer values using GCD.

What is Greatest common Divisior (GCD)?


In Mathematics, the Greatest Common Divisor (GCD) of two or more integers is the
largest positive integer that divides given integer values. For example, the GCD
value of integer 8 and 12 is 4 because both 8 and 12 are divisible by 1, 2, and 4
(the remainder is 0), and the largest positive integer among them is 4.

The Greatest Common Divisor (GCD) is also known as Highest Common Factor
(HCF), or Greatest Common Factor (GCF), or Highest Common Divisor (HCD), or
Greatest Common Measure (GCM).
CLASS 11 COMPUTER SCIENCE (083) LAB PROGRAMS--- Thriveni

What is Least Common Multiple(LCM)?


In Mathematics, the Least Common Multiple (LCM) of two or more integers is the
smallest positive integer that is totally divisible by the given integer
values.Remainder should be zero after division. For example, the LCM value of
integer 2 and 3 is 12 because 12 is the smallest positive integer that is divisible by
both 2 and 3 (the remainder is 0).

The least common multiple is also known as lowest common multiple,


or smallest common multiple of two integers.

Below is source code


1# python program to find LCM of two number using GCD
2
3#input two numbers
4n1 = int(input("Enter First number :"))
5n2 = int(input("Enter Second number :"))
6x = n1
7y = n2
8while(n2!=0):
9 t = n2
10 n2 = n1 % n2
11 n1 = t
12gcd = n1
13print("GCD of {0} and {1} = {2}".format(x,y,gcd))
14lcm = (x*y)/gcd
15print("LCM of {0} and {1} = {2}".format(x,y,lcm))
Output:
>> %Run gcdlcm.py
Enter First number :54
Enter Second number :24
GCD of 54 and 24 = 6
LCM of 54 and 24 = 216.0

>>> %Run gcdlcm.py


Enter First number :4
Enter Second number :6
GCD of 4 and 6 = 2
LCM of 4 and 6 = 12.0

>>> %Run gcdlcm.py


CLASS 11 COMPUTER SCIENCE (083) LAB PROGRAMS--- Thriveni

Enter First number :125


Enter Second number :25
GCD of 125 and 25 = 25
LCM of 125 and 25 = 125.0
Below is Snapshot of python program

Explaination:
This python program allows the user to enter two positive integer values n1 and
n2. We declared two variables x and y and assigned value of n1 and n2 to them.
We used while loop to check the remainder of n1 % n2 and n2 is equals to zero or
not. If true, n1 is calculated. After that, value of n1 is assigned to GCD. With the
help of GCD, we can calculate LCM of two integer. Here We used mathematical
formula to calculate LCM.

First,we multiplied two positive integers and then divided by gcd to compute LCM
of two integer.

PROGRAM 10 ::::Python program to Count and display the


number of vowels consonants, uppercase, lowercase characters
and digits in string
CLASS 11 COMPUTER SCIENCE (083) LAB PROGRAMS--- Thriveni

This program uses ASCII values to find vowels and consonants and uses python
inbuilt string function to find upper case and lowercase letter. It also check
whether string contains digit or alphabets. If User specified string contain
alphabets and numbers then it shows mixed string otherwise it counts total number
of digits , uppercase and lowercase letters.

This python program counts number of vowels and consonants from user specified
string. It also counts number of uppercase and lower case letter and digits if
present in user entered string.At the end it returns total number of
vowels,consonants,u

ppercase and lowercase letter and digits in numbers.


CLASS 11 COMPUTER SCIENCE (083) LAB PROGRAMS--- Thriveni

Below is snapshot of above code


Python program to count vowels,consonants,uppercase,lowercase and digits

PROGRAM 11::: input a string and determine whether it is


palindrome or not; convert the case of characters in python
python program to check for palindrome string using for and if-else loop

Output
Enter any string :mam
CLASS 11 COMPUTER SCIENCE (083) LAB PROGRAMS--- Thriveni

mam --> This string is palindrome


String after converting the case of each character : MAM

Enter any string :SuJata


SuJata --> This string is not palindrome
String after converting the case of each character : sUjATA

PROGRAM 12:: Python program to find the largest/ smallest


number in a list/tuple
We can Sort the list using sort method and Sort element in ascending order and
print the first element and last in the list. We can also use min() and max()
function to find largest and smallest element in list
CLASS 11 COMPUTER SCIENCE (083) LAB PROGRAMS--- Thriveni

Output
>>> %Run smalllargeinlist.py
Enter number of elements in list: 5
Enter elements: 56
Enter elements: 44
Enter elements: 33
Enter elements: 77
Enter elements: 22
Smallest element in List1 is: 22
Largest element in List1 is: 77

Enter number of elements in list: 10


Enter elements: 55
Enter elements: 77
Enter elements: 33
Enter elements: 88
Enter elements: 99
Enter elements: 22
Enter elements: 45
Enter elements: 67
Enter elements: 34
Enter elements: 52
Smallest element in List is: 22
Largest element in List is: 99

PROGRAM 13::: Python program to swap elements at even


location with elements at odd location
Here,We are going to write python program to input a list of numbers and swap
element at even location with the elements at the odd location in python. We can
create the list of numbers and find out even and odd location of elements in list.
Even and odd locations are also called as even and odd indices.

Suppose we have a list of numbers called numlist, we will exchange each


consecutive even indexes with each other, and also exchange each consecutive
odd index with each other.

So, if the input is like [11,22,33,44,55,66,77,88,99], then elements at even


location are 22,44,66,88 and elements at odd locations are 11,33,55,77,99. After
exchanging the location of list elements ,output will be [’22’, ’11’, ’44’, ’33’, ’66’,
’55’, ’88’, ’77’, ’99’]
CLASS 11 COMPUTER SCIENCE (083) LAB PROGRAMS--- Thriveni
CLASS 11 COMPUTER SCIENCE (083) LAB PROGRAMS--- Thriveni

Output
Total List Items to enter = 9
Enter the 1 List Item = 11
Enter the 2 List Item = 22
Enter the 3 List Item = 33
Enter the 4 List Item = 44
Enter the 5 List Item = 55
Enter the 6 List Item = 66
Enter the 7 List Item = 77
Enter the 8 List Item = 88
Enter the 9 List Item = 99
Original list : ['11', '22', '33', '44', '55', '66', '77', '88', '99']
Elements at odd location : ['11', '33', '55', '77', '99']
Elements at even location : ['22', '44', '66', '88']
List after swapping : ['22', '11', '44', '33', '66', '55', '88', '77', '99']

PROGRAM 14:::Input a list/tuple of elements, search for given


element in the list/tuple in python

here,We are going to write python program to input a list of elements and search
for given element in the list. We can easily check the existence of element in list
using count() method. By using count method,We can check the number of times
element exists in list.

python program to search given elements in list


CLASS 11 COMPUTER SCIENCE (083) LAB PROGRAMS--- Thriveni

Output
otal List Items to enter = 5
Enter the 0 List Item = 3
Enter the 1 List Item = 6
Enter the 2 List Item = 7
Enter the 3 List Item = 8
Enter the 4 List Item = 9
Enter any element that you want to search :9

9 is found

Total List Items to enter = 5


Enter the 0 List Item = 88
Enter the 1 List Item = 33
Enter the 2 List Item = 56
Enter the 3 List Item = 77
Enter the 4 List Item = 78
Enter any element that you want to search :22
22 is not found in list:

PROGRAM 15::Create a dictionary with roll number, name and marks of n students in a
class and display the names of students who have scored marks above 75 in python

 Here ,We are going to create a dictionary with roll number, name and marks
of n students in a class and display the names of students who have scored
marks above 75 in python. We can use for loop to enter number of students
in dictionary. With the help of if else loop,we can check the number of
students who got more than 75 percentage marks .
CLASS 11 COMPUTER SCIENCE (083) LAB PROGRAMS--- Thriveni

Python program to create dictionary

Output:

Enter number of students :3


Enter Details of students : 1
Roll number :3
Name :Sumedh
Enter English Marks: 77
Enter Math score: 88
Enter Computer Marks: 99
Enter Physics Marks: 67
Enter Chemistry Marks: 56
Enter Details of students : 2
Roll number :2
Name :Sushil
Enter English Marks: 78
Enter Math score: 55
Enter Computer Marks: 66
Enter Physics Marks: 44
Enter Chemistry Marks: 77
Enter Details of students : 3
Roll number :8
Name :Anita
CLASS 11 COMPUTER SCIENCE (083) LAB PROGRAMS--- Thriveni

Enter English Marks: 66


Enter Math score: 22
Enter Computer Marks: 11
Enter Physics Marks: 44
Enter Chemistry Marks: 33
{3: ['Sumedh', 77.4], 2: ['Sushil', 64.0], 8: ['Anita', 35.199999999999996]}
Following students having more than 75 marks:
Sumedh

PROGRAM 16: # Python program to find the factorial of a


number provided by the user.

# To take input from the user


num = int(input("Enter a number: "))

factorial = 1

# check if the number is negative, positive or zero


if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
CLASS 11 COMPUTER SCIENCE (083) LAB PROGRAMS--- Thriveni

PROGRAM 17:: # Python program to Display calendar

import calendar
# ask of month and year
yy = int(input("Enter year: "))
mm = int(input("Enter month: "))
# display the calendar
print(calendar.month(yy,mm))

Program 18: Python Program to Find ASCII value


of a character
K = input("Please enter a character: ")
print ("The ASCII value of '" + K + "' is ", ord(K))

In the above code, we have used the ord() function for converting a character into an
integer, that is, ASCII value. This function is used to return the Unicode code point of the
given character.

ASCII: ASCII is an acronym that stands for American Standard Code for Information
Interchange. A specific numerical value is given to different characters and symbols
for computers to store and manipulate in ASCII.

It is case sensitive. The same character, having different formats (upper case and
lower case), has a different value. For example, The ASCII value of "A" is 65 while the
ASCII value of "a" is 97.
CLASS 11 COMPUTER SCIENCE (083) LAB PROGRAMS--- Thriveni

Program 19: Python Program to check if a


Number is Positive, Negative or Zero

Here, we will learn to write a Python program through which we can check if the
given number is positive, zero, or negative.

Positive Numbers: A number is said to be positive if the number has a value greater
than 0, like 1, 2, 3, 5, 7, 9, 11, 13, etc. All the natural numbers are positive numbers.

Negative Numbers: If a given number has a value less than 0 like -1, -2, -3, -5, -7, -9,
-11, -13, etc., then we can say that the given number is a negative number. Only
rational and integer type numbers can have negative values or numbers.

Example:
1. # Default function to run if else condition
2. def NumberCheck(a):
3. # Checking if the number is positive
4. if a > 0:
5. print("Number given by you is Positive")
6. # Checking if the number is negative
7. elif a < 0:
CLASS 11 COMPUTER SCIENCE (083) LAB PROGRAMS--- Thriveni

8. print("Number given by you is Negative")


9. # Else the number is zero
10. else:
11. print("Number given by you is zero")
12. # Taking number from user
13. a = float(input("Enter a number as input value: "))
14. # Printing result
15. NumberCheck(a)

Output:

Enter a number as input value: -6


Number given by you is Negative

Explanation:

We have used a nested if else condition in the program to check the number. When
the user gives an input number, the program will first check if the value of the
number is greater than 0 (if yes, it will print positive and the program ends),
otherwise it will check if the value is less than 0 and it last it will print that number is
0.

Program 20: Python Program to Find Common


Characters in Two Strings
Problem Description
The program takes two strings and checks common letters in both the strings.

Problem Solution
1. Enter two input strings and store it in separate variables.
2. Convert both of the strings into sets and find the common letters between both
the sets.
3. Store the common letters in a list.
4. Use a for loop to print the letters of the list.
5. Exit.

Program/Source Code
CLASS 11 COMPUTER SCIENCE (083) LAB PROGRAMS--- Thriveni

Here is source code of the Python Program to check common letters in the two
input strings. The program output is also shown below.

s1=raw_input("Enter first string:")


s2=raw_input("Enter second string:")
a=list(set(s1)&set(s2))
print("The common letters are:")
for i in a:
print(i)
Program Explanation
1. User must enter two input strings and store it in separate variables.
2. Both of the strings are converted into sets and the common letters between both
the sets are found using the ‘&’ operator.
3. These common letters are stored in a list.
4. A for loop is used to print the letters of the list.

Runtime Test Cases

Case 1:
Enter first string:Hello
Enter second string:How are you
The common letters are:
H
e
o

Case 2:
Enter first string:Test string
Enter second string:checking
The common letters are:
i
e
g
n

You might also like