0% found this document useful (0 votes)
52 views20 pages

Document From Sakthi

Uploaded by

gokul.at.study
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)
52 views20 pages

Document From Sakthi

Uploaded by

gokul.at.study
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/ 20

12TH CS PROGRAM

Program 1: Input any number from user and calculate factorial of a number
AIM:
To write the Program to calculate factorial of entered number
Algorithm:
Step 1 : Take a number from the user.
Step 2. Initialize a factorial variable to 1.
Step 3. Use a while loop to multiply the number to the factorial variable and then
decrement the number.
Step 4. Continue this till the value of the number is greater than 1.
Step 5. Print the factorial of the number.
Step 6. Exit.
Source code:

Output:

RESULT:
Thus the above program is executed and output is verified successfully.
EX-2

PALINDROME

AIM: To write a program to check a number whether it is palindrome or not.

ALGORITHM:-

Step 1: Take the input and store in a variable.

Step 2: Use a temporary variable with the same input value for reserve.

Step 3: Use a while loop to store each digit (using modulus) and reverse it
(multiply by 10 and add the removed digit to the current reversed number).

Step 4: Remove the last digit from the input.

Step 5: The reversed number is then compared with the input value stored in
the temporary variable.

Step 6: If both are equal, then the input is a palindrome number, else it is
not.

SOURCE CODE:

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


n=num
res=0
while num>0:
rem=num%10
res=rem+res*10
num=num//10
if res==n:
print("Number is Palindrome")
else:
print("Number is not Palindrome")

OUTPUT:
EX-3

COMPOUND INTEREST

AIM : To write a program to calculate compound interest.

ALGORITHM:-

Step 1: Start
Step 2: Input Principal, Time and Rate
Step 4: Compound Interest; CI = P * ((1 + R/100) ** T -1)
Step 5: Display Compound Interest; CI
Step 6: Print the result

SOURCE CODE:
p=float(input("Enter the principal amount : "))
r=float(input("Enter the rate of interest : "))
t=float(input("Enter the time in years : "))

x=(1+r/100)**t

CI= p*x-p
print("Compound interest is : ", round(CI,2))

OUTPUT:
EX:4

SIMPLE CALCULATOR

AIM:-
To write a python program to perform the operations of a simple calculator
based on the choice given by the user.

ALGORITHM:-

1 .Initialize choice to “Y”


2. Get the values of “a”,”b” and “op” from the user.
3. Based on the operator given by the user, perform the operation and store it in
variable “c”.
4. Get the value for “choice” from the user.
5. If the value of “choice” is “y”, then repeat steps 2,3 and 4 else stop the
execution.

PROGRAM:-

import subprocess
cls = subprocess.call('cls',shell=True)
choice = "y"
while choice == "y" or choice =="Y":
a=int(input("Enter 1st no "))
b=int(input("Enter 2nd no "))
op=input("Enter the operator (+,-,,/) ")
if(op=="+"):
c=a+b
print("Sum = ",c)
elif(op==""):
c=a*b
print("Product = ",c)
elif(op=="-"):
if(a>b):
c=a-b
else:
c=b-a
print("Difference = ",c)
elif(op=="/"):
if b!= 0:
c=a/b
print("Division = ",c)
else:
print("Division not possible")
else:
print("Invalid operator")
print ()
choice=input("Do you want to continue y/n")

OUTPUT:-

Enter 1st no 12
Enter 2nd no 56
Enter the operator (+,-,,/) +
Sum = 68

Do you want to continue y/ny


Enter 1st no 78
Enter 2nd no 8
Enter the operator (+,-,,/) -

Do you want to continue y/ny


Enter 1st no 100
Enter 2nd no 10
Enter the operator (+,-,,/) /
Division = 10.0

Do you want to continue y/n n


EX – 5 ARMSTRONG NUMBER

AIM :-

To write a python program to check if the given number is an armstrong


number or not.

ALGORITHM:-

1. Get the number “n” from the user.

2. Initialize “n1” to “n” and “s” to 0.

3. Repeat steps 4 to 6 until n>0

4. Divide the number “n” by 10 and store the remainder in “d”.

5. Calculate the cube of “d” and add the result to “s”.

6. Divide “n” by 10.

7. If s=n1 then display “ Armstrong Number “ else display “ Not an Armstrong


Number”

PROGRAM:-

n=int(input("Enter the number to check : "))


n1=n
s=0
while(n>0):
d=n%10
s=s + (d *d * d)
n=int(n/10)
if s==n1:
print("Armstrong Number")
else:
print("Not an Armstrong Number")
OUTPUT:-

Enter the number to check : 370


Armstrong Number

Enter the number to check : 570


Not an Armstrong Number
Program 6: Input any number from user and check it is Prime no. or not
AIM:
To write the Program to input any number from user and Check it is Prime number of
not.
Algorithm:
Step 1: Start the program import math library
Step 2: Take the input from the user
Step 3: Initialize a for loop starting from 2 ending at the integer value of the floor of the
square root of the number.
Step 4: Check if the number is divisible by 2
Step 5: Repeat till the square root of the number is checked for.
Step 6: In case, the number is divisible by any of the numbers, the number is not prime
Step 7: Else, it is a prime number

Source code:

Output:

RESULT:
Thus the above program is executed and output is verified successfully.
Program 7: Write a program to calculate the nth term of Fibonacci series

Aim:
To write the program to calculate the nth term of Fibonacci series.
Algorithm:
Step 1. Take the number of terms from the user and store it in a variable.
Step 2. Pass the number as an argument to a recursive function named nthfiboterm.
Step 3. Define the base condition as the number to be lesser than or equal to 1.
Step 4. Otherwise call the function recursively with the argument as the number minus 1
added to the function called recursively with the argument as the number minus 2.
Step 5. Print the returned value which is the fibonacci series.
6. Exit.
Source code:

Output:

RESULT:
Thus the above program is executed and output is verified successfully.
Program 8 : Program to search any word in given string/sentence

Aim:
To write the Program to find the occurence of any word in a string
Algorithm:

Step 1: Define the function CountWord which takes str1 id input string and word is the
word count occurrence.
Step 2: Split the string by space.
Step 3: We use one counter variable as count and it’s initialized by 0 and if word is match
then count is incremented by 1.
Step 4: Then we searching using for loop.
Step 5: If condition is true then counter count is increased and display the final result.

Source code:

Output:

RESULT:
Thus the above program is executed and output is verified successfully.
Program 9: Program to read and display file content line by line with each word
separated by “#‟

Aim:
To write the Program to read content of file line by line and display each word
separated by '#'
Algorithm:

Step 1: Opening the text file


Step 2. Read each line from the file and split the line to form a list of words.
step 3. Use a for loop to traverse through the words in the list and another for loop to
traverse through the letters in the word..
step 4. A for loop is used to traverse through the words list and another for loop is used to
traverse through the letters in the word.
Step 5: display the final result it printed each word separated by '#'

Source code:

NOTE : if the original content of file is:


India is my country
I love python
Python learning is fun
Output:

RESULT:
Thus the above program is executed and output is verified successfully.
Program 10: Program to read the content of file and display the total number of
consonants, uppercase, vowels and lower case characters‟

Aim:
To write the Program to read content of file and display total number of vowels,
consonants, lowercase and uppercase characters.
Algorithm:
Step 1: Opening the text file
Step 2. Initialize the variables to 0.
Step 3: Read all the contents of file and store in a string variable named data using read
function.
step 4. Use a for loop to traverse through the characters in the string and increment the
first count variable each time a lowercase character is encountered and increment the
second count variable each time a uppercase character is encountered.
Step 5 :Print the total count of the variables.
Step 6. Exit.

Source code:

Note:
NOTE : if the original content of file is:
India is my country
I love python
Python learning is fun
123@

Output:

RESULT:
Thus the above program is executed and output is verified successfully.
Program 11: Program to create binary file to store Rollno and Name, Search any Rollno
and display name if Rollno found otherwise “Rollno not found”

Aim:
To write the Program to create a binary file to store Rollno and name Search for
Rollno and display record if found otherwise "Roll no. not found"

Algorithm:
Step 1: Start the program by efficiently using pickle module .
Step 2: Create the empty list
Step3: Open file in binary mode for writing and reading the records
Step 4: Serialize the object and writing to the file
Step 5: Read the end of file
Step 6: Searching the record based on roll number.
Step 7: Read the object from file and display the object if roll number matched otherwise
it display not found.
Step 8 : Close the file

Source code:

Output:
RESULT:
Thus the above program is executed and output is verified successfully.
Program 12 : Program to create binary file to store Rollno,Name and Marks and
update marks of entered Rollno

Aim:
To write the Program to create binary file to store Rollno,Name and Marks and
update marks of entered Rollno

Algorithm:
Step 1: Start the program by efficiently using pickle module .
Step 2: Create the empty list
Step3: Open file in binary mode for writing and reading the records
Step 4: Serialize the object and writing to the file
Step 5: Read the end of file
Step 6: Update the record based on roll number.
Step 7: Read the object from file and Update the object if roll number matched otherwise
it display not found.
Step 8 : Close the file
Source code

Output:
RESULT:
Thus the above program is executed and output is verified successfully.
Program 13: Program to read the content of file line by line and write it to another file
except for the lines contains “a‟ letter in it.

Aim:
To write the Program to read line from file and write it to another line Except for
those line which contains letter 'a'

Algorithm:
Step 1: Open the file name file3.txt in read mode and assign into f1
Step 2: Open other file name file3copy.txt in write mode and assign into f2.
Step 3: Read the content line by line in the file except for those lines which contains letter
“a” in it.
Step 4: In order to write the remaining line in f2.
Step 5: Display the result
Step 6: Close all the files
Source code:

Note:
NOTE: Content of file3.txt

a quick brown fox


one two three four
five six seven
India is my country
eight nine ten
bye!

Output:

Note:
After copy content of file3copy.txt

one two three four


five six seven
eight nine ten
bye!
RESULT:
Thus the above program is executed and output is verified successfully.
Program 14: Program to create CSV file and store empno,name,salary and search any
empno and display name,salary and if not found appropriate message.

Aim:
To write the to create CSV file and store empno,name,salary and search any empno
and display name,salary and if not found appropriate message.

Algorithm:
Step 1: Importing csv module
Step 2: Reading the csv file with append (Open a file for write only) mode
Step 3: Writing to csv file with delimiter and assign in mywriter
Step 4: Take the input data from user and saved the data
Step 5: Reading the csv file with read(Open a file for read only) mode.
Step 6: Writing to csv file with delimiter and assign in myreader
Step7: Searching the record based on roll number.
Step 8: Read the object from file and display the object if roll number matched otherwise
it display not found.

Source code:

Output:
RESULT:
Thus the above program is executed and output is verified successfully.
Program 15: Program to generate random number 1-6, simulating a dice

Aim:
To write the Program to generate random number 1-6, simulating a dice
Algorithm:
Step 1: Importing random and time modules.
Step 2: Use a while loop that runs while the value of a variable which we will call again,
is true.
Step 3: Inside the try block , Use for loop to generate the number, if any of these throw
an error the except block of code will execute.
Step 4: We can use the randint() function in the random library to generate the random
numbers from 1 to 6
Step 5: The except block of code will have a print statement to display the number of
dice.
Step 6: The break command can be executed to break out of the loop and the program
will end.
Source code:

Output:
Press CTRL+C to stop the dice

Your Number is : 4
Play More? (Y) : y

Your Number is : 3
Play More? (Y) : y

Your Number is : 2
Play More? (Y) : n
RESULT:
Thus the above program is executed and output is verified successfully.

You might also like