0% found this document useful (0 votes)
36 views7 pages

Sivangi Ass

Uploaded by

kumari492620
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)
36 views7 pages

Sivangi Ass

Uploaded by

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

Q1 Write a program in python to check a number whether it is prime or not.

num = int(input("Enter the number "))


for a in range(2,num+1):
prime = True
for b in range(2,int((a+2)/2)):
r =int(a%b)
if (r==0):
prime = False
break
if (prime):
print(f"{a} is a prime number")
else:
print(f"{a} is not a Prime number")

OUTPUT:-
OUTPUT:-
Enterthe
Enter thenumber
number47
47
47 is a Prime number
47 is a Prime number

Q2 Write a program to check a number whether it is palindrome or not.


def is_palindrome(num):
original_num = num
reverse_num = 0

while num > 0:


remainder = num % 10
reverse_num = reverse_num * 10 + remainder
num = num // 10

# Check if the original number is equal to its reverse


if original_num == reverse_num:
return True
else:
return False

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


if is_palindrome(number):
print(f"{number} is a palindrome")
else:
print(f"{number} is not a palindrome")

OUTPUT:-
OUTPUT:-
OUTPUT:-
Entera anumber:
Enter number:14541
14541
14541 is a palindrome
14541 is a palindrome

Q3 Write a function to swap the values of two variables through a function.


def swap_values(a, b):
temp = a
a=b
b = temp
return a, b
# Test the function
x = 55
y = 100
print("Before swapping the value of X and Y:")
print("x =", x)
print("y =", y)

x, y = swap_values(x, y)

print("After swapping the value of X and Y :")


print("x =", x)
print("y =", y)

OUTPUT:-
OUTPUT:-
Before
Before swapping
swapping thethe value
value of Xofand
X and
Y: Y:
x =x55
= 55
y =y100
= 100
After swapping
After thethe
swapping value of Xofand
value Y ::Y ::
X and
x = 100
y =x55
= 100
y = 55

Q4 Write a python program to Read a file line by line and print it.
def file_name(file):
file = open(file,’r’)
while True:
line = file.read()
if not line :
break
print(line)

name = input("Enter the name of file : ")


file_name(name)

OUTPUT
OUTPUT
Enter
Enter thethe filename:
filename: p11.txt
p11.txt
mymy name is shivangi
live in vidisha
live in vidisha

Q5 Write a program to display the number of lines in the file and size of a file in bytes.

def file_info(filename):
try:
with open(filename, ’r’) as file:
num_lines = sum(1 for line in file)
file.seek(0, 2) # Move the file pointer to the end
file_size = file.tell() # Get the size of the file
print("Number of lines:", num_lines)
print("Size of the file (in bytes):", file_size)
except FileNotFoundError:
print("File not found.")
# Displaying the number of lines and size of a file
filename = input("Enter the filename: ")
file_info(filename)

OUTPUT
OUTPUT
Enter thethe
Enter filename: pramid.c
filename: pramid.c
Number of lines: 55
Number
Size of lines:
of the file 55 1040
(in bytes):
Size of the file (in bytes): 1040

Q6 Write a program to calculate the factorial of an integer using recursion.

def factorial(a):
if(a<0):
print("you enter the negative value")
elif (a==0):
return 1
else :
return a*factorial(a-1)
try:
n= int(input("Enter your number "))
print("The factorial of the number is ",factorial(n))
except (ValueError):
print("The input value is not valide ")

OUTPUT
OUTPUT
EnterEnter your number
your number 6 6
The factorial
The factorial of the of the number
number is 720 is 720

Q7 Write a program to print Fibonacci series using recursion.


def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)

def fib_series(terms):
if terms <= 0:
print("Please enter a positive integer.")
else:
print("Fibonacci Series:")
for i in range(terms):
print(fibonacci(i), end=" ")

terms = int(input("Enter the number of terms for Fibonacci series: "))


fib_series(terms)

OUTPUT
OUTPUT
Enter
Enter the the number
number of terms
of terms for Fibonacci
for Fibonacci series:series:
8 8
Fibonacci
Fibonacci Series:
Series:
0 1 01 12132538513
8 13
Q8 Write a program for binary search.
def binary_search(arr, target):
low = 0
high = len(arr) - 1

while low <= high:


mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1

return -1 # Return -1 if the target is not found

# Test the function


arr = [2, 4, 6, 8, 11, 12, 13, 19, 18, 20]
target = int(input("Enter the your target value "))
result = binary_search(arr, target)

if result != -1:
print(f"Element {target} is present at index {result}.")
else:
print(f"Element {target} is not present in the array.")

OUTPUT:-
OUTPUT:-
Enter the your target value 19
Enter the your target value 19
Element1919isispresent
Element presentat at index
index 7. 7.

Q9 Python Program for Sum of squares of first n natural numbers.

def sum_of_squares(n):
if n < 0:
return "Please enter a non-negative integer."
else:
return n * (n + 1) * (2 * n + 1) // 6

# Test the function


n = int(input("Enter a positive integer n: "))
result = sum_of_squares(n)
print(f"The sum of squares of the first {n} natural numbers is: {result}")

OUTPUT:-
OUTPUT:-
Enter
Enter aa positive
positive integer
integer n: 9n: 9
The sum of squares of the
The sum of squares of the firstfirst
9 natural numbers
9 natural is: 285
numbers is: 285
Q10 Python Program to find sum of array.

arr = [1, 2, 3, 4, 5]
array_sum = sum(arr)
print("The sum of the array elements is:", array_sum)

OUTPUT:-
OUTPUT:-
The
The sum
sum of the
of the array
array elements
elements is: 15is: 15

Q11 Python program to read character by character from a file.


def read_file_character_by_character(filename):
try:
with open(filename, ’r’) as file:
while True:
char = file.read(1)
if not char:
break
print(char, end=’’)
except FileNotFoundError:
print("File not found.")

# Test the function


filename = input("Enter the name of the file: ")
read_file_character_by_character(filename)
OUTPUT:-
OUTPUT:-
Enterthe
Enter the name
name of of
thethe
file:file: p10.py
p10.py
arr= =[1,[1,2,2,3,3,4,4,5]5]
arr
array_sum= =sum(arr)
array_sum sum(arr)
print("The sum
print("The sum of of
thethe array
array elements
elements is:", is:", array_sum)
array_sum)

Q12 Python Program to print with your own font.


def decode(s):
char_to_num = {
’ ’: ’ ’, ’A’: ’01’, ’a’: ’01’, ’B’: ’02’, ’b’: ’02’, ’C’: ’03’, ’c’: ’03’,
’D’: ’04’, ’d’: ’04’, ’E’: ’05’, ’e’: ’05’, ’F’: ’06’, ’f’: ’06’, ’G’: ’07’,
’g’: ’07’, ’H’: ’08’, ’h’: ’08’, ’I’: ’09’, ’i’: ’09’, ’J’: ’10’, ’j’: ’10’,
’K’: ’11’, ’k’: ’11’, ’L’: ’12’, ’l’: ’12’, ’M’: ’13’, ’m’: ’13’, ’N’: ’14’,
’n’: ’14’, ’O’: ’15’, ’o’: ’15’, ’P’: ’16’, ’p’: ’16’, ’Q’: ’17’, ’q’: ’17’,
’R’: ’18’, ’r’: ’18’, ’S’: ’19’, ’s’: ’19’, ’T’: ’20’, ’t’: ’20’, ’U’: ’21’,
’u’: ’21’, ’V’: ’22’, ’v’: ’22’, ’W’: ’23’, ’w’: ’23’, ’X’: ’24’, ’x’: ’24’,
’Y’: ’25’, ’y’: ’25’, ’Z’: ’26’, ’z’: ’26’
}
for i in s:
print(char_to_num.get(i, ’’), end=’’)

a = input("enter your sentence \n")


decode(a)

OUTPUT:-
OUTPUT:-
enter
enter your
your sentence
sentence
my name is shivangi
1325 14011305 0919 19080922011409
Q13 Python program to print even length words in a string.
def print_even_length_words(string):
words = string.split()
even_length_words = [word for word in words if len(word) % 2 == 0]
print("Even length words in the string:")
for word in even_length_words:
print(word)

# Test the function


string = input("Enter a string: ")
print_even_length_words(string)
OUTPUT:-
OUTPUT:-
Enteraastring:
Enter string: what
what areareyouyou doing
doing bestbest friend
friend
Evenlength
Even lengthwords
wordsin in
thethe string:
string:
what
what
best
best
friend
Q14 Python program to check if a string is palindrome or not.
def is_palindrome(string):
# Remove spaces and convert to lowercase
string = string.replace(" ", "").lower()
# Check if the string is equal to its reverse
return string == string[::-1]

# Test the function


string = input("Enter a string: ")
if is_palindrome(string):
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")
OUTPUT:-
OUTPUT:-
Enter a string: fool
The string is not a palindrome.

Q15 Program to print ASCII Value of a character.


def ascii_value(character):
# ord() function returns the ASCII value of a character
return ord(character)

character = input("Enter a character: ")


print("The ASCII value of ’{}’ is: {}".format(character, ascii_value(character)))

OUTPUT:-
OUTPUT:-
Enter
Enter a character:
a character: J J
The
The ASCII value ’J’
ASCII value of of is: 74 74
'J' is:
Q16 Python program to find smallest and largest number in a list.
def find(numbers):
if not numbers:
return None, None
smallest = largest = numbers[0]
for num in numbers:
if num < smallest:
smallest = num
elif num > largest:
largest = num
return smallest, largest

# Test the function


numbers = [12, 45, 7, 23, 56, 89, 2, 90, 34]
smallest, largest = find(numbers)
print("Smallest number:", smallest)
print("Largest number:", largest)

OUTPUT:-
OUTPUT:-
Smallest
Smallest number:
number: 2 2
Largest number: 90
Largest number: 90
Q17 Python program to find the size of a Tuple.
import sys

def tuple_size(t):
return sys.getsizeof(t)

# Test the function


my_tuple = (1, 2, 3, 4, 5)
print("Size of the tuple:", tuple_size(my_tuple))

OUTPUT:-
OUTPUT:-
Size of the tuple: 80
Size of the tuple: 80

You might also like