Singur Programs (1)
Singur Programs (1)
#Write a program using function in python to check a number whether it is prime number or not
def PrimeChecker(n):
if n>1:
for i in range(2,int(n/2)+1):
if(n%i==0):
break
else:
else:
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):
input_str=str(input_str).lower()
input_str=input_str.replace(" ","")
return input_str==input_str[::-1]
number=int(input("Enter a number"))
if is_palindrome(number):
print(f"{number} is a Palindrome")
else:
if is_palindrome(string):
print(f"{string} is a Palindrome")
else:
Program: 3
#Write a program using function to display ascii code of a character and vice versa
Program : 4
def calculate_lcm(x,y):
i=2
if x>y:
lg=x
else:
lg=y
lcm=lg
while(True):
return(lcm)
lcm=lg*i
i=i+1
Program : 5
#Source Code
def calculate_hcf(x,y):
i=2
if x>y:
hcf=y
else:
hcf=x
while(True):
return(hcf)
hcf=hcf-1
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:
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)
Program :8
#Write a Program to generate random number between 0 and 9 and check whether a user won
lottery or not
import random
a=random.randint(0,1)
print(a)
if(a==x):
else:
print("Bad luck. Wish you a better luck in next time")
else:
print("Invalid number")
Program : 9
f1=open("file1.txt","w")
f1.writelines(lines)
f1.close()
Program 10:
#Write a Python Program using function to count the number of words in a file
count=0
fp=open("file1.txt","r")
words=line.split(" ")
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:
count=count+1
print(count)
Program:12
fp=open("file1.txt","r")
line=" "
while line:
line=fp.readline()
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
pickle.dump(string,fh)
#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=""
st=pickle.load(fh)
lst=st.split('o')
print(lst[0])