0% found this document useful (0 votes)
13 views4 pages

python (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)
13 views4 pages

python (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/ 4

# 1.

Program to reverse a string without using built-in functions

def reverse_string(input_str):
reversed_str = ""
for char in input_str:
reversed_str = char + reversed_str # Prepend each character to form the reversed
string
return reversed_str

# Input from the user


input_str = input("Enter a string to reverse: ")
result = reverse_string(input_str)
print("Reversed String:", result)

# 2. Swap two numbers without using a third variable

def swap_numbers(a, b):


a=a+b
b=a-b
a=a-b
return a, b

# Input from the user


num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
num1, num2 = swap_numbers(num1, num2)
print("After swapping: First Number =", num1, "Second Number =", num2)

# 3. Check if a vowel is present in a string

def contains_vowel(input_str):
vowels = "aeiouAEIOU"
for char in input_str:
if char in vowels:
return True
return False

# Input from the user


input_str = input("Enter a string to check for vowels: ")
if contains_vowel(input_str):
print("The string contains at least one vowel.")
else:
print("The string does not contain any vowels.")
# 4. Check if a number is a prime number

def is_prime(num):
if num <= 1:
return False
for i in range(2, num):
if num % i == 0:
return False
return True

# Input from the user


number = int(input("Enter a number to check if it is prime: "))
if is_prime(number):
print(number, "is a prime number.")
else:
print(number, "is not a prime number.")

# 5. Print a Fibonacci sequence using recursion

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

# Input from the user


terms = int(input("Enter the number of terms for the Fibonacci sequence: "))
print("Fibonacci Sequence:")
for i in range(terms):
print(fibonacci(i), end=" ")
print()

# 6. Check if a list contains only odd numbers

def all_odd(numbers):
for num in numbers:
if num % 2 == 0:
return False
return True

# Input from the user


size = int(input("Enter the number of elements in the list: "))
num_list = []
print("Enter the elements:")
for _ in range(size):
num_list.append(int(input()))

if all_odd(num_list):
print("The list contains only odd numbers.")
else:
print("The list contains at least one even number.")

# 7. Check if a string is a palindrome

def is_palindrome(input_str):
length = len(input_str)
for i in range(length // 2):
if input_str[i] != input_str[length - i - 1]:
return False
return True

# Input from the user


input_str = input("Enter a string to check if it is a palindrome: ")
if is_palindrome(input_str):
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")

# 8. Remove spaces from a string

def remove_spaces(input_str):
no_space_str = ""
for char in input_str:
if char != " ":
no_space_str += char
return no_space_str

# Input from the user


input_str = input("Enter a string to remove spaces: ")
result = remove_spaces(input_str)
print("String without spaces:", result)

# 9. Remove leading and trailing spaces from a string

def trim_spaces(input_str):
start = 0
end = len(input_str) - 1
while start <= end and input_str[start] == " ":
start += 1
while end >= start and input_str[end] == " ":
end -= 1

trimmed_str = ""
for i in range(start, end + 1):
trimmed_str += input_str[i]

return trimmed_str

# Input from the user


input_str = input("Enter a string to trim spaces: ")
result = trim_spaces(input_str)
print("String after trimming spaces:", result)

# 10. Sort an array without using built-in functions

def bubble_sort(arr):
n = len(arr)
for i in range(n - 1):
for j in range(n - i - 1):
if arr[j] > arr[j + 1]:
# Swap elements
arr[j], arr[j + 1] = arr[j + 1], arr[j]

# Input from the user


size = int(input("Enter the number of elements in the array: "))
arr = []
print("Enter the elements:")
for _ in range(size):
arr.append(int(input()))

bubble_sort(arr)
print("Sorted Array:", arr)

You might also like