0% found this document useful (0 votes)
19 views

Singur Programs (1)

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)
19 views

Singur Programs (1)

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/ 8

Program:1

#Write a program using function in python to check a number whether it is prime number or not

#A function for Prime checking conditions

def PrimeChecker(n):

if n>1:

#Iterating over the given number with for loop

for i in range(2,int(n/2)+1):

#If the given number is divisible or not

if(n%i==0):

print(n," is not a Prime number")

break

#Else it is a Prime number

else:

print("It is a Prime number")

#If the given number is 1

else:

print(n," is not a Prime number")

#Taking an Input number from the user

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

#Printing Result

PrimeChecker(a)

Program: 2

#Write a program using function to check whether a number and string whether it is Pallindrome or
not.

#Source code:
def is_palindrome(input_str):

#Convert input to lowercase for case-insensitive comparison(for strings)

input_str=str(input_str).lower()

#Remove spaces from the input(for strings)

input_str=input_str.replace(" ","")

#Check the reversed string is equal to the original string

return input_str==input_str[::-1]

#Example 1: Check if a number is Palindrome

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

if is_palindrome(number):

print(f"{number} is a Palindrome")

else:

print(f"{number} is not a Palindrome")

#Example 2: Check if a String is palindrome

string=input("Enter a string to check whether it is a Palindrome or not")

if is_palindrome(string):

print(f"{string} is a Palindrome")

else:

print(f"{string} is not a Palindrome")

Program: 3

#Write a program using function to display ascii code of a character and vice versa

k=input("Please Enter a character ")

print("The ASCII value of "+k+" is ",ord(k))

a=int(input("Enter an ASCII value"))

print("The character is ",chr(a))

Program : 4

#Write a Python program using function to find L.C.M.


#Defining a function to calculate L.C.M.

def calculate_lcm(x,y):

i=2

#Selecting the greater number

if x>y:

lg=x

else:

lg=y

lcm=lg

while(True):

if(lcm%x==0 and lcm%y==0):

return(lcm)

lcm=lg*i

i=i+1

#Taking Input from user

num1=int(input("Enter first number"))

num2=int(input("Enter another number"))

#Printing the L.C.M.

print("L.C.M. of ",num1," and ",num2," is ",calculate_lcm(num1,num2))

Program : 5

#Write a Python program using function to calculate H.C.F.

#Source Code

#Defining a function to calculate H.C.F.

def calculate_hcf(x,y):

i=2

#Selecting the smallest number

if x>y:
hcf=y

else:

hcf=x

while(True):

if(x%hcf==0 and y%hcf==0):

return(hcf)

hcf=hcf-1

#Taking Input from user

num1=int(input("Enter first number"))

num2=int(input("Enter another number"))

#Printing the H.C.F.

print("H.C.F. of ",num1," and ",num2," is ",calculate_hcf(num1,num2))

Program : 6

#Write a Program in Python to input a character and detect whether it is an alphabet,digit or any
other character

ch=input("Enter a character")[0]

if ch.isalpha()==True:

print("It is an alphabet")

elif ch.isdigit()==True:

print("It is a digit")

else:

print("It is a special character")

Program : 7

#Write a python program using function sin(x,n)to calculate the value of sin(x) using taylor series
expansion upto n terms.

import math
def factorial(num):

if num==0 or num==1:

return 1

else:

return num*factorial(num-1)

def sin_taylor_series(x,n):

result=0

for i in range(n):

term=((-1)**i)*(x**(2*i+1))/factorial(2*i+1)

result+=term

return result

x_value=math.pi/4

terms=5

result_sin=sin_taylor_series(x_value,terms)

print(f" The value of sin({x_value})using {terms}terms of the Taylor series is approximately


{result_sin}")

Program :8

#Write a Program to generate random number between 0 and 9 and check whether a user won
lottery or not

import random

x=int(input("Enter your single digit ticket number"))

if(x>=0 and x<=9):

a=random.randint(0,1)

print(a)

if(a==x):

print("You won the lottery")

else:
print("Bad luck. Wish you a better luck in next time")

else:

print("Invalid number")

Program : 9

#Write a Program to write a file.

f1=open("file1.txt","w")

lines=["Hello World"," Welcome to the world of Python","Enjoy Learning"]

f1.writelines(lines)

f1.close()

print("data written to file")

Program 10:

#Write a Python Program using function to count the number of words in a file

count=0

#opens file in read mode

fp=open("file1.txt","r")

#gets each line till end of file is reached

for line in fp:

#splits each line into words

words=line.split(" ")

#counts each word

count=count+len(words)

print(str(count))

fp.close()

Program : 11
#W.A.P. in Python to count no of vowels in the file "file1.txt"

fin=open("file1.txt","r")

str=fin.read()

count=0

for i in str:

if(i=='a' or i=='e' or i=='i' or i=='o' or i=='u'):

count=count+1

print(count)

Program:12

# W.A.P. to add a # after each word of a file

fp=open("file1.txt","r")

line=" "

while line:

line=fp.readline()

for word in line.split():

print(word,end="#")

print()

fp.close()

Program: 13

#Create a binary file namely myfile.info and write a string having two lines in it.

import pickle

string="This is my first line. This is second line."

with open("myfile.info","wb") as fh:

pickle.dump(string,fh)

print("File successfully created")


Program : 14

#Program to read from the file myfile.info and display the string until letter 'o' is encountered. i.e. ,
display all the text before the letter'o'.

import pickle

st=""

with open("myfile.info","rb") as fh:

st=pickle.load(fh)

lst=st.split('o')

print(lst[0])

You might also like