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

Interview Programs

The document contains various Python code snippets demonstrating algorithms for checking prime numbers, identifying palindromes, reversing strings, generating Fibonacci series, calculating factorials, and counting vowels in a string. Each section includes both iterative and recursive implementations for some functions. The code is structured to provide examples of how to perform these tasks effectively.

Uploaded by

swapnarks1650
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Interview Programs

The document contains various Python code snippets demonstrating algorithms for checking prime numbers, identifying palindromes, reversing strings, generating Fibonacci series, calculating factorials, and counting vowels in a string. Each section includes both iterative and recursive implementations for some functions. The code is structured to provide examples of how to perform these tasks effectively.

Uploaded by

swapnarks1650
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

PRIME OR NOT

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

if n <= 1:

print("Not Prime")

else:

is_prime = True

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

if n % i == 0:

is_prime = False

break

if is_prime:

print("Prime")

else:

print("Not Prime")

RECURRSION

def is_prime_recursive(n, i=2):

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:

print(f"{num} is not a prime number.")

PRIME IN RANGE:

lower = 900

upper = 1000

print(f"Prime numbers between {lower} and {upper} are:")

for num in range(lower, upper + 1):

if num > 1:

is_prime = True

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

if num % i == 0:

is_prime = False

break

if is_prime:

print(num, end=' ')

PRIME NUMBER UPTO N

import math

n = int(input("Enter the value of n: "))

print(f"Prime numbers up to {n} are:")

for num in range(2, n + 1):

is_prime = True

for i in range(2, int(math.sqrt(num)) + 1):

if num % i == 0:

is_prime = False

break
if is_prime:

print(num, end=' ')

PALINDROME OF A STRING USING 2 POINTER MODEL

s = "malayalam" # string

i,j = 0, len(s) - 1 # two pointers

is_palindrome = True # assume palindrome

while i < j:

if s[i] != s[j]: # mismatch found

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

while temp > 0:

remainder = temp % 10

reverse = (reverse * 10) + remainder

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

print("Fibonacci Series:", n1, n2, end=" ")

for i in range(2, num):

n3 = n1 + n2

n1 = n2

n2 = n3

print(n3, end=" ")

FIBONACCI SERIES USING RECURRSION:

def fibonacci(n):

if n <= 1:

return n

else:

return fibonacci(n-1) + fibonacci(n-2)


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

if terms <= 0:

print("Please enter a positive integer")

else:

print("Fibonacci sequence:")

for i in range(terms):

print(fibonacci(i))

FACTORIAL

n= 6

fact = 1

for i in range(1, n + 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:

#take user input

String = input('Enter the string :')

count = 0
#to check for less conditions

#keep string in lowercase

String = String.lower()

for i in String:

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

#if True

count+=1

#check if any vowel found

if count == 0:

print('No vowels found')

else:

print('Total vowels are :' + str(count))

You might also like