Interview Programs
Interview Programs
if n <= 1:
print("Not Prime")
else:
is_prime = True
if n % i == 0:
is_prime = False
break
if is_prime:
print("Prime")
else:
print("Not Prime")
RECURRSION
if n <= 1:
return False
if i > int(n//2):
return True
if n % i == 0:
return False
return is_prime_recursive(n, i + 1)
# Example usage
num = 29
if is_prime_recursive(num):
print(f"{num} is a prime number.")
else:
PRIME IN RANGE:
lower = 900
upper = 1000
if num > 1:
is_prime = True
if num % i == 0:
is_prime = False
break
if is_prime:
import math
is_prime = True
if num % i == 0:
is_prime = False
break
if is_prime:
s = "malayalam" # string
while i < j:
is_palindrome = False
break
i += 1
j -= 1
if is_palindrome:
print("Yes")
else:
print("No")
PALINDROME OF A NUMBER:
num = 1221
temp = num
reverse = 0
remainder = temp % 10
temp = temp // 10
if num == reverse:
print('Palindrome')
else:
print("Not Palindrome")
REVERSE A STRING:
def reverse_string(s):
reversed_s = ''
for char in s:
reversed_s = char + reversed_s
return reversed_s
string = "hello"
reversed_string = reverse_string(string)
print(reversed_string) # Output: olleh
FIBONACCI SERIES:
num = 10
n1, n2 = 0, 1
n3 = n1 + n2
n1 = n2
n2 = n3
def fibonacci(n):
if n <= 1:
return n
else:
if terms <= 0:
else:
print("Fibonacci sequence:")
for i in range(terms):
print(fibonacci(i))
FACTORIAL
n= 6
fact = 1
fact *= i
print(fact)
USING RECURSION
def fact(n):
if (n==1 or n==0):
return 1
else:
return n * fact(n - 1)
# Driver Code
num = 5
print(fact(num))
VOWELS IN A STRING:
count = 0
#to check for less conditions
String = String.lower()
for i in String:
#if True
count+=1
if count == 0:
else: