Python Outputs For Finding Words, Lines, Characters
Python Outputs For Finding Words, Lines, Characters
NO:
SWAPPING TWO NUMBERS (USING THIRD VARIABLE)
DATE:
AIM:
To write a program in python to swap two numbers using third variable.
ALGORITHM:
Step 1: Start the program.
Step 2: Read the two numbers a and b.
Step 3: Perform
t a
ab
bt
Step 4: Print the values a and b.
Step 5: Stop the program.
PROGRAM:
File name: swapping.py
print("$$ SWAPPING TWO NUMBERS USING THIRD VARIABLE $$")
a=int(input("Enter value of a:"))
b=int(input("Enter value of b:"))
print("Before Swapping a=",a,"b=",b)
t=a
a=b
b=t
print("After Swapping a=",a,"b=",b)
OUTPUT:
RESULT:
Thus a python program to swap two numbers using third variable has been
executed successfully and output got verified.
EX.NO:
SWAPPING (WITHOUT USING THIRD VARIABLE)
DATE:
AIM:
To write a program in python to swap two numbers without using third
variable.
ALGORITHM:
Step 1: Start the program.
Step 2: Read the two numbers a and b.
Step 3: Perform
aa+b
ba–b
aa–b
Step 4: Print the values a and b.
Step 5: Stop the program.
PROGRAM:
File Name: swapping.py
print("$$$ SWAPPING TWO NUMBERS WITHOUT USING THIRD
VARIABLE $$$")
a=int(input("Enter value of a:"))
b=int(input("Enter value of b:"))
print("Before Swapping a=",a,"b=",b)
a=a+b
b=a-b
a=a-b
print("After Swapping a=",a,"b=",b)
OUTPUT:
RESULT:
Thus a python program to swap two numbers without using third variable
has been executed successfully and output got verified.
EX.NO:
CELSIUS TO FAHRENHEIT
DATE:
AIM:
To write a program in python to convert the Celsius value to Fahrenheit.
ALGORITHM:
Step 1: Start the program
Step 2: Read the Celsius Value
Step 3: Calculate the Fahrenheit value using the Formula
Fahrenheit ( 1.8 * Celsius ) +32
Step 4: Print the Fahrenheit value
Step 5: Stop the program
PROGRAM:
File Name: ctof.py
print("$$$ CELSIUS TO FAHRENHEIT CONVERSION $$$")
c=int(input("Enter the value of Celsius:"))
f=(1.8 * c ) + 32
print("Fahrenheit=",f)
OUTPUT:
RESULT:
Thus a python program to convert the Celsius value to Fahrenheit has been
executed successfully and output got verified.
EX.NO:
AREA AND CIRCUMFERENCE OF THE CIRCLE
DATE:
AIM:
To write a program in python to find the area and circumference of the
circle.
ALGORITHM:
Step 1: Start the program
Step 2: Read the radius of circle
Step 3: Calculate circle area 3.14 * r * r
Step 4: Calculate circle circumference 2 * 3.14 * r
Step 5: Stop the program
PROGRAM:
File Name: area.py
print("$$$ AREA AND CIRCUMFERENCE OF THE CIRCLE $$$")
r=int(input("Enter the value of radius:"))
area=3.14*r*r
circum=2*3.14*r
print("Area=",area,"and circumference=",circum)
OUTPUT:
RESULT:
Thus a python program to find the area and circumference of the circle has
been executed successfully and output got verified.
EX.NO:
EVEN OR ODD
DATE:
AIM:
To write a program in python to check whether the given number is even or
odd.
ALGORITHM:
Step 1: Start the program.
Step 2: Read the value of n.
Step 3: If n % 2 == 0 then
Print n is even.
Otherwise
Print n is odd.
Step 4: Stop the program.
PROGRAM:
File name: even.py
print("$$$ EVEN OR ODD $$$")
n=int(input("Enter value of n:"))
if n%2==0:
print(n,"is even")
else:
print(n,"is odd")
OUTPUT:
RESULT:
Thus a python program to check whether the given number is even or odd
has been executed successfully and output got verified.
EX.NO:
LEAP YEAR OR NOT
DATE:
AIM:
To write a program in python to check whether the given year is leap year or
not.
ALGORITHM:
Step 1: Start the program.
Step 2: Read the value for year.
Step 3:If year % 4 = = 0 then
Print the given year is leap year.
Otherwise
Print the given year is non - leap year.
Step 4: Stop the program.
PROGRAM:
File Name: leap.py
print("$$$ LEAP YEAR OR NOT $$$")
year=int(input("Enter the year:"))
if year % 4 = = 0:
print(year,"is a leap year")
else:
print(year,"is not a leap year")
OUTPUT:
RESULT:
Thus a python program to check whether the given year is leap year or not
has been executed successfully and output got verified.
EX.NO:
BIGGEST AMONG TWO NUMBERS
DATE:
AIM:
To write a program in python to find biggest among two numbers.
ALGORITHM:
Step 1: Start the program.
Step 2: Read the value of a and b.
Step 3: if a > b then
Print a is big.
else
Print b is big.
Step 4: Stop the program.
PROGRAM:
File Name: big.py
print("$$$ BIGGEST AMONG TWO NUMBERS $$$")
a=int(input("Enter the value of a:"))
b=int(input("Enter the value of b:"))
if a>b:
print(a,"is bigger")
else:
print(b,"is bigger")
OUTPUT:
RESULT:
Thus a python program to find biggest among two numbers has been
executed successfully and output got verified.
EX.NO:
BIGGEST AMONG THREE NUMBERS
DATE:
AIM:
To write a program in python to find biggest among three numbers.
ALGORITHM:
Step 1: Start the program.
Step 2: Read the value of a, b and c.
Step 3: if a > b and a > c then
Assign big a
else if b > a and b > c then
Assign big b
else
Assign big c
Step 4: Print big.
Step 5: Stop the program.
PROGRAM:
File Name: biggest.py
print("$$$ BIGGEST AMONG THREE NUMBERS $$$")
a=int(input("Enter the value of a:"))
b=int(input("Enter the value of b:"))
c=int(input("Enter the value of c:"))
if a>b and a>c:
big=a
elif b>a and b>c:
big=b
else:
big=c
print("The biggest number is=",big)
OUTPUT:
RESULT:
Thus a python program to find biggest among three numbers has been
executed successfully and output got verified.
EX.NO:
ROOTS OF QUADRATIC EQUATION
DATE:
AIM:
To write a program in python to find the roots of a quadratic equation.
ALGORITHM:
Step 1: Start the program.
Step 4:
Print R1 and R2
R2 = ( -b – sqrt ((d) ) / (2 * a)
Print R1 and R2
x = (-b) / (2 * a)
y=√-d/ (2*a)
Print them in the format of ( x + iy ), ( x – iy )
PROGRAM:
File Name: roots.py
import math
print("$$$ ROOTS OF QUADRATIC EQUATION $$$")
a=int(input("Enter the value of a:"))
b=int(input("Enter the value of b:"))
c=int(input("Enter the value of c:"))
d=(b*b)-(4*a*c)
if d==0:
print("roots are real and equal")
r1=r2=(-b)/(2*a)
print("The roots are:",r1,r2)
elif d>0:
print("roots are real and unequal");
r1=(-b+(math.sqrt(d)))/(2*a);
r2=(-b-(math.sqrt(d)))/(2*a);
print("The roots are:",r1,r2);
else:
print("The roots are imaginary")
r1=(-b)/(2*a)
r2=(math.sqrt(-d))/(2*a)
print("The roots are:",r1,"+i",r2,"and",r1,"-i",r2);
OUTPUT:
RESULT:
Thus a python program to find the roots of a quadratic equation has been
executed successfully and output got verified.
EX.NO:
SUM OF FIRST N NATURAL NUMBERS
DATE:
AIM:
To write a program in python to find the sum of first n natural numbers.
ALGORITHM:
Step 1: Start the program.
Step 2: Read the number n.
Step 3: Initialize the i 1 and sum 0.
Step 4: while i <= n do
Calculate sum sum + i
Increment i i+1
End
Step 5: Print the sum value.
Step 6: Stop the program.
PROGRAM:
File name: natural.py
print("$$$ SUM OF N NATURAL NUMBERS $$$")
n=int(input("Enter the value of n:"))
i=1
sum=0
while i<=n:
sum=sum+i
i=i+1
print("Sum of",n,"natural numbers=",sum)
OUTPUT:
RESULT:
Thus a python program to find the sum of first n natural numbers has been
executed successfully and output got verified.
EX.NO:
FACTORIAL OF A NUMBER
DATE:
AIM:
To write a program in python to find the factorial of a number.
ALGORITHM:
Step 1: Start the program.
Step 2: Read the number n.
Step 3: Initialize the i 1 and fact 1.
Step 4: while i <= n do
Calculate fact fact * i
Increment i i+1
End
Step 5: Print the fact value.
Step 6: Stop the program.
PROGRAM:
File name: factorial.py
print("$$$ FACTORIAL OF A NUMBER $$$")
n=int(input("Enter the value of n:"))
i=1
fact=1
while i<=n:
fact=fact*i
i=i+1
print("The factorial of",n,"is=",fact)
OUTPUT:
RESULT:
Thus a python program to find the factorial of a number has been executed
successfully and output got verified.
EX.NO:
FIBONACCI SERIES
DATE:
AIM:
To write a program in C to print the first n terms of the Fibonacci series.
ALGORITHM:
Step 1: Start the program.
Step 2: Read the value for n.
Step 3: Assign a 1 , b 0 , c 0 , i 1
Step 4: while i <= n do
Print c
Calculate c = a + b
Process the assignment a b and b c
Increment i i+1
End
Step 5: Stop the program.
PROGRAM:
File name: fibonacci.py
print("$$$ FIBONACCI SERIES $$$")
n=int(input("Enter the value of n:"))
a=1
b=0
c=0
i=1
while i<=n:
print(c)
c=a+b
a=b
b=c
i=i+1
OUTPUT:
RESULT:
Thus a python program to print the first n terms of the Fibonacci series has
been executed successfully and output got verified.
EX.NO:
SUM OF DIGITS
DATE:
AIM:
To write a program in python to find the sum of the digits of given
number.
ALGORITHM:
Step 1: Start the program.
Step 2: Read the value for n.
Step 3: Initialize sum 0.
Step 4: While the number n is not equal to zero do
3.1. d the remainder of n divided by 10
3.2. n n /10
3.3. sum sum + d
Step 5: Print the result for sum of the digits.
Step 6: Stop the program.
PROGRAM:
File name: sumofdigits.py
print("$$$ SUM OF DIGITS IN A NUMBER $$$")
n=int(input("Enter the value of n:"))
sum=0
while n!=0:
d=n%10
n=n//10
sum=sum+d
print("Sum of digits=",sum)
OUTPUT:
RESULT:
Thus a python program to find the sum of the digits of given number has
been executed successfully and output got verified.
EX.NO:
REVERSE OF A NUMBER
DATE:
AIM:
To write a program in python to find the reverse of the given number.
ALGORITHM:
Step 1: Start the program
Step 2: Read the value for n
Step 3: Initialize rev 0
Step 4: While the number, n is not less than zero do
3.1. d the remainder of n divided by 10
3.2. n n /10
3.3. rev rev * 10 + d
Step 5: Print the reverse of n rev
Step 6: Stop the program
PROGRAM:
File name: reverse.py
print("$$$ REVERSE THE NUMBER $$$")
n=int(input("Enter the value of n:"))
rev=0
while n!=0:
d=n%10
n=n//10
rev=rev*10+d
print("Revere form=",rev)
OUTPUT:
RESULT:
Thus a python program to find the reverse of the given number has been
executed successfully and output got verified.
EX.NO:
PALINDROME OR NOT
DATE:
AIM:
To write a program in python to check whether the given number is
palindrome or not.
ALGORITHM:
Step 1: Start the program.
Step 2: Read the value for n.
Step 3: Assign t n and rev 0
Step 4: While the number, n is not less than zero do
3.1. d the remainder of n divided by 10
3.2. n n /10
3.3. rev rev * 10 + d
Step 5: if rev == t then
Print the given number is palindrome.
else
Print the given number is not palindrome.
Step 6: Stop the program.
PROGRAM:
File name: palindrome.py
print("$$$ PALINDROME OR NOT $$$")
n=int(input("Enter the value of n:"))
rev=0
t=n
while n!=0:
d=n%10
n=n//10
rev=rev*10+d
if rev==t:
print(rev,"is a palindrome")
else:
print(rev,"is not a palindrome")
OUTPUT:
RESULT:
Thus a python program to check whether the given number is palindrome or
not has been executed successfully and output got verified.
EX.NO:
ARMSTRONG NUMBER OR NOT
DATE:
AIM:
To write a program in python to check whether the given number is
Armstrong number or not.
ALGORITHM:
Step 1: Start the program.
Step 2: Read the value for n.
Step 3: Assign t n and arms 0
Step 4: While the number, n is not less than zero do
3.1. d the remainder of n divided by 10
3.2. n n /10
3.3. arms arms + d * d * d
Step 5: if arms == t then
Print the given number is an Armstrong number.
else
Print the given number is not an Armstrong number.
Step 6: Stop the program.
PROGRAM:
File name: armstrong.py
print("$$$ ARMSTRONG NUMBER OR NOT $$$")
n=int(input("Enter the value of n:"))
arms=0
t=n
while n!=0:
d=n%10
n=n//10
arms=arms+d*d*d
if arms==t:
print(arms,"is an armstrong number")
else:
print(arms,"is not an armstrong number")
OUTPUT:
RESULT:
Thus a python program to check whether the given number is Armstrong
number or not has been executed successfully and output got verified.
EX.NO:
STRONG NUMBER OR NOT
DATE:
AIM:
To write a program in python to check whether the given number is strong
number or not.
ALGORITHM:
Step 1: Start the program.
Step 2: Read the value for n.
Step 3: Assign t n and arms 0
Step 4: While the number, n is not equal to zero do
3.1. d the remainder of n divided by 10
3.2. n n /10
3.3. initialize i f 1
3.4. while i <= d do
3.4.1. f f * i
3.4.2. i i + 1
3.5. compute sum sum + f
Step 5: if sum == t then
Print the given number is a strong number.
else
Print the given number is not a trong number.
Step 6: Stop the program.
PROGRAM:
File name: strong.py
print("$$$ STRONG NUMBER OR NOT $$$")
n=int(input("Enter the value of n:"))
sum=0
t=n
while n!=0:
d=n%10
n=n//10
i=1
f=1
while i<=d:
f=f*i
i=i+1
sum=sum+f
if sum==t:
print(sum,"is s strong number")
else:
print(sum,"is not a strong number")
OUTPUT:
RESULT:
Thus a python program to check whether the given number is strong number
or not has been executed successfully and output got verified.
EX.NO:
PERFECT NUMBER OR NOT
DATE:
AIM:
To write a program in python to check whether the given number is perfect
number or not.
ALGORITHM:
Step 1: Start the program.
Step 2: Read the value for n.
Step 3: Assign s 0 and i 1
Step 4: While the number, i is less than n do
3.1. if n % i == 0 then
ss+i
3.2. increment i i + 1
Step 5: if s == n then
Print the given number is a perfect number.
else
Print the given number is not a perfect number.
Step 6: Stop the program.
PROGRAM:
File name: perfect.py
print("$$$ PERFECT NUMBER OR NOT $$$")
n=int(input("Enter the value of n:"))
s=0
i=1
while i<n:
if n%i==0:
s=s+i
i=i+1
if s==n:
print(n,"is a perfect number")
else:
print(n,"is not a perfect number")
OUTPUT:
RESULT:
Thus a python program to check whether the given number is perfect
number or not has been executed successfully and output got verified.
EX.NO:
PRIME NUMBER OR NOT
DATE:
AIM:
To write a program in python to check whether the given number is prime
or not.
ALGORITHM:
Step 1: Start the program.
Step 2: Read the value for n.
Step 3: Initialize i 2 and c 0.
Step 4: while i <= n / 2 do
4.1. if n % i == 0 then
c1
break
4.2. i i + 1
Step 5: If c == 0 then
Print the given number is a prime number
else
Print the given number is not a prime number
Step 6: Stop the program
PROGRAM:
File name: prime.py
print("$$$ PRIME NUMBER OR NOT $$$")
n=int(input("Enter the value of n:"))
i=2
c=0
while i<=n/2:
if n%i==0:
c=1
break
i=i+1
if c==0:
print(n,"is a prime number")
else:
print(n,"is not a prime number")
OUTPUT:
RESULT:
Thus a python program to check whether the given number is prime or not
has been executed successfully and output got verified.
EX.NO:
FACTORIAL OF A NUMBER USING PREDEFINED FUNCTION
DATE:
AIM:
To write a program in python to find factorial of a number using
predefined function.
ALGORITHM:
Step 1: Start the program.
Step 2: Import math module.
Step 3: Read the value for n.
Step 4: compute factorial value by calling predefined factorial function.
Step 5: Print the factorial value.
Step 6: Stop the program
PROGRAM:
File name: fact.py
import math
print("$$$ FACTORIAL USING PREDEFINED FUNCTION $$$")
n=int(input("Enter a number:"))
f=math.factorial(n)
print("The factorial of",n,"is",f)
OUTPUT:
RESULT:
Thus a python program to find factorial of a number using predefined
function has been executed successfully and output got verified.
EX.NO: BASIC ARITHMETIC OPERATION USING USER DEFINED
FUNCTION
DATE:
AIM:
To write a program in python to perform basic arithmetic operation using
user defined function.
ALGORITHM:
Step 1: Start the program.
Step 2: Read the value of a and b.
Step 3: call user defined functions such as add(),sub(),mul(),div() and
mod().
Step 4:Print the results.
Step 5: Stop the program.
PROGRAM:
File name: arithmetic.py
def add(x,y):
return x+y
def sub(x,y):
return x-y
def mul(x,y):
return x*y
def div(x,y):
return x/y
def mod(x,y):
return x%y
RESULT:
Thus a python program to perform basic arithmetic operation using user
defined function has been executed successfully and output got verified.
EX.NO:
FACTORIAL USING RECURSION
DATE:
AIM:
To write a program in python to find factorial of a given number using
recursion.
ALGORITHM:
Step 1: Start the program.
Step 2: Read the value of n.
Step 3: Call the recursive function factorial, with n as an argument.
Step 4: Print the returned value of a function factorial.
Step 5: Stop the program.
Function factorial(a)
Step 1: Start the function.
Step 2: if a = = 0 then
return 1
else
call it recursively with N-1 as an argument and return it.
Step 3: Stop the function.
PROGRAM:
File Name: fact.py
def factorial(a):
if a==0:
return 1
else:
return (a*factorial(a-1))
OUTPUT:
RESULT:
Thus a python program to calculate the factorial of a number using recursive
function and the program has been executed successfully and output got verified.
EX.NO:
FIBONACCI SERIES USING RECURSION
DATE:
AIM:
To write a program in python using recursion generate the fibonacci series
up to for the given number.
ALGORITHM:
Step 1: Start the program.
Step 2: Read the value of n.
Step 3: for i 0 to n do
Call the recursive function fibonacci with i as arguments.
Step 4: Print the returned value of the function fibonacci.
Step 5: Stop the program
Function fibonacci (i)
Step 1: Start the function.
Step 2: If i is equal to zero then
return 0
if i is equal to 1 then
return 1
Step 3: Call the recursive function finonacci with i-1 as an argument and
return it.
Step 4: Stop the function.
PROGRAM:
File name: fibonacci.py
def fibonacci(i):
if(i == 0):
return 0
if(i == 1):
return 1
return (fibonacci(i-1) + fibonacci(i-2))
print("$$$ FIBONACCI SERIES USING RECURSION $$$")
n=int(input("Enter the value of n:"))
print("The Fibonacci Series is:")
for i in range(0,n):
print(fibonacci(i))
OUTPUT:
RESULT:
Thus a python program to print the Fibonacci series using recursive function
and the program has been executed successfully and output got verified.
EX.NO:
TOWER OF HANOI USING RECURSION
DATE:
AIM:
To write a program in python to solve Tower of Hanoi problem using
recursive function.
ALGORITHM:
Step 1: Start the program.
Step 2: Read number of disks.
Step 3: call the recursive function movedisk with passing num, source,
destination and intermediate as arguments.
Step 4: Stop the program
Function movedisk(n,fromtower,totower,auxtower)
Step 1: Start the function.
Step 2: if n is equal to one then
Print disk movement from source to destionation.
otherwise
2.1: call the recursive function movedisk with passing n-1,
fromtower,auxtower and totower as arguments.
2.2: Print disk movement from source to destionation through
intermediate.
2.3: call the recursive function movedisk with passing n-1,
auxtower, totower and fromtower as arguments.
Step 3: Stop the function.
PROGRAM:
File name: hanoi.py
def movedisk(n,fromtower,totower,auxtower):
if n==1:
print("Move disk",n,"from",fromtower,"to",totower)
else:
movedisk(n-1,fromtower,auxtower,totower)
print("Move disk",n,"from",fromtower,"to",totower)
movedisk(n-1,auxtower,totower,fromtower)
print("$$$ TOWER OF HANOI USING RECURSION $$$")
num=int(input("How many Disk:"))
print("The moves are:")
movedisk(num,'S','D','I')
print("Note:\n S - Source\n D - Destination\n I - Intermediate")
OUTPUT:
RESULT:
Thus a python program to solve tower of Hanoi problem using recursive
function has been executed successfully and output got verified.
EX.NO:
UNIQUE ELEMENTS IN A LIST
DATE:
AIM:
To write a program in python to find unique elements in a list.
ALGORITHM:
Step 1: Start the program.
Step 2: Read upper limit of the list.
Step 3: Read all the elements one by one by using for loop.
Step 4: For i 1 to n do
s1
for j 0 to n do
if list[i] = = list[j] and i ! = j then
s0
if s = = 1 then
Print the unique elements in a list.
Step 5: Stop the program.
PROGRAM:
File Name: unique.py
print("$$$ UNIQUE ELEMENTS IN A LIST $$$")
list=[]
print("Enter Upper Limit:")
n=int(input())
print("Enter the numbers:")
for i in range(0,n):
a=int(input())
list.append(a)
print("The unique elements are:")
for i in range(0,n):
s=1
for j in range(0,n):
if((list[i]==list[j])and(i!=j)):
s=0
if(s==1):
print(list[i])
OUTPUT:
RESULT:
Thus a python program to find unique elements in a list has been executed
successfully and output got verified.
EX.NO:
MATRIX ADDITION
DATE:
AIM:
To write a program in python to perform addition of two matrices.
ALGORITHM:
Step 1: Start the program.
Step 2: Read the values of matrix x and y.
Step 3: Initialize the result matrix as zero.
Step 4: for i 0 to len(x)
for j 0 to len(x[0])
Perform result[i][j] x[i][j] + y[i][j]
Step 5: Print the resultant matrix.
Step 6: Stop the program.
PROGRAM:
File name: matrixadd.py
print("$$$ MATRIX ADDITION $$$")
x = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
y = [[5,8,1],
[6,7,3],
[4,5,9]]
result = [[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(len(x)):
for j in range(len(x[0])):
result[i][j] = x[i][j] + y[i][j]
RESULT:
Thus a python program to perform matrix addition has been executed
successfully and output got verified.
EX.NO: SUM OF TWO NUMBERS USING COMMAND
DATE: LINE ARGUMENT
AIM:
To write a program in python to find sum of two numbers using command
line argument.
ALGORITHM:
Step 1: Start the program.
Step 2: Import sys module.
Step 3: Read the input from the command line.
Step 4: perform addition operation.
Step 5: Print the result.
Step 6: Stop the program.
PROGRAM:
File name: sum.py
import sys
print("$$$ SUM OF TWO NUMBERS USING COMMAND LINE
ARGUMENT $$$")
a=int(sys.argv[1])
b=int(sys.argv[2])
c=a+b
print("Sum=",c)
OUTPUT:
RESULT:
Thus a python program to find sum of two numbers using command line
argument has been executed successfully and output got verified.
EX.NO:
FILE COPY
DATE:
AIM:
To write a program in python to copy content from one file to another file.
ALGORITHM:
Step 1: Start the program.
Step 2: Open text.txt file in read mode.
Step 3: Using read() method read all the contents from file.
Step 4: Open sam.txt file in write mode.
Step 5: using write() method write the contents to sam.txt.
Step 6: Close the files.
Step 6: Stop the program.
PROGRAM:
File name: filecopy.py
print("$$$ FILE COPY $$$")
r=open("test.txt","r")
str=r.read()
w=open("sam.txt","w")
w.write(str)
print("File content copied")
r.close()
w.close()
OUTPUT:
RESULT:
Thus a python program to copy content from one file to another file has been
executed successfully and output got verified.