CLASS 11 LAB PRG-complete
CLASS 11 LAB PRG-complete
-
//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: 2) 1 -x + x² – x³ + … + xⁿ
CLASS 11 COMPUTER SCIENCE (083) LAB PROGRAMS--- Thriveni
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
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:
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
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 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
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.
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
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.
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
Output
Enter any string :mam
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
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']
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.
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
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
Output:
factorial = 1
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))
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
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
Output:
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.
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.
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