Basic Programming Questions (Logic-Based)
Check if a number is prime:
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5)+1):
if n % i == 0:
return False
return True
num = int(input("Enter a number: "))
print("Prime" if is_prime(num) else "Not Prime")
Reverse a given number:
num = int(input("Enter a number: "))
rev = 0
while num != 0:
rev = rev * 10 + num % 10
num //= 10
print("Reversed number:", rev)
Find the largest element in an array:
arr = list(map(int, input("Enter elements: ").split()))
print("Largest element:", max(arr))
Print Fibonacci series up to n terms:
n = int(input("Enter number of terms: "))
a, b = 0, 1
for _ in range(n):
print(a, end=' ')
a, b = b, a + b
Check if a string is a palindrome:
s = input("Enter a string: ")
print("Palindrome" if s == s[::-1] else "Not Palindrome")
Calculate the sum of digits of a number:
num = int(input("Enter a number: "))
total = 0
while num:
total += num % 10
num //= 10
print("Sum of digits:", total)
Find the GCD (HCF) of two numbers:
import math
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("GCD:", math.gcd(a, b))
Find the LCM of two numbers:
import math
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
lcm = abs(a*b) // math.gcd(a, b)
print("LCM:", lcm)
Count the number of vowels in a string:
s = input("Enter string: ")
vowels = 'aeiouAEIOU'
count = sum(1 for ch in s if ch in vowels)
print("Number of vowels:", count)
Find the second largest number in an array:
arr = list(map(int, input("Enter array elements: ").split()))
arr = list(set(arr))
arr.sort()
print("Second largest:", arr[-2])
Intermediate Programming Questions
Check if two strings are anagrams:
s1 = input("Enter first string: ")
s2 = input("Enter second string: ")
print("Anagrams" if sorted(s1) == sorted(s2) else "Not Anagrams")
Remove all duplicates from a list:
lst = list(map(int, input("Enter list elements: ").split()))
unique = list(dict.fromkeys(lst))
print("List after removing duplicates:", unique)
Count frequency of elements in an array:
arr = list(map(int, input("Enter array elements: ").split()))
freq = {}
for num in arr:
freq[num] = freq.get(num, 0) + 1
print("Frequencies:", freq)
Swap two numbers without a temp variable:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
a, b = b, a
print("Swapped values: a =", a, "b =", b)
Print triangle pattern:
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
print('*' * i)
Sort array using Bubble Sort:
arr = list(map(int, input("Enter array: ").split()))
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
print("Sorted array:", arr)
Find factorial using recursion:
def factorial(n):
if n == 0 or n == 1:
return 1
return n * factorial(n - 1)
num = int(input("Enter number: "))
print("Factorial:", factorial(num))
Implement Binary Search:
def binary_search(arr, key):
low, high = 0, len(arr)-1
while low <= high:
mid = (low + high) // 2
if arr[mid] == key:
return mid
elif arr[mid] < key:
low = mid + 1
else:
high = mid - 1
return -1
arr = sorted(list(map(int, input("Enter sorted array: ").split())))
key = int(input("Enter element to search: "))
index = binary_search(arr, key)
print("Found at index" if index != -1 else "Not found")
Sum of all even numbers in a list:
lst = list(map(int, input("Enter numbers: ").split()))
even_sum = sum(x for x in lst if x % 2 == 0)
print("Sum of even numbers:", even_sum)
Calculate average of n numbers:
n = int(input("Enter count: "))
total = sum(int(input("Enter number: ")) for _ in range(n))
print("Average:", total / n)
Advanced / Logic-Based Coding Practice
Find first non-repeating character in a string:
s = input("Enter a string: ")
freq = {}
for ch in s:
freq[ch] = freq.get(ch, 0) + 1
for ch in s:
if freq[ch] == 1:
print("First non-repeating:", ch)
break
else:
print("No non-repeating character")
Find missing number from 1 to n:
arr = list(map(int, input("Enter array: ").split()))
n = len(arr) + 1
total = n * (n + 1) // 2
print("Missing number:", total - sum(arr))
Move all zeros to the end:
arr = list(map(int, input("Enter array: ").split()))
result = [x for x in arr if x != 0] + [0] * arr.count(0)
print("After moving zeros:", result)
Rotate array by k positions:
arr = list(map(int, input("Enter array: ").split()))
k = int(input("Enter k: "))
k %= len(arr)
rotated = arr[-k:] + arr[:-k]
print("Rotated array:", rotated)
Basic calculator using if-else:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
op = input("Enter operator (+ - * /): ")
if op == '+': print(a + b)
elif op == '-': print(a - b)
elif op == '*': print(a * b)
elif op == '/': print(a / b if b != 0 else "Divide by zero")
else: print("Invalid operator")
Check if array is sorted:
arr = list(map(int, input("Enter array: ").split()))
print("Sorted" if all(arr[i] <= arr[i+1] for i in range(len(arr)-1)) else "Not sorted")
Reverse each word in a sentence:
s = input("Enter sentence: ")
print(' '.join(word[::-1] for word in s.split()))
Generate multiplication table:
n = int(input("Enter number: "))
for i in range(1, 11):
print(f"{n} x {i} = {n * i}")
Decimal to Binary and Binary to Decimal:
choice = input("1 for Decimal to Binary, 2 for Binary to Decimal: ")
if choice == '1':
d = int(input("Enter decimal: "))
print("Binary:", bin(d)[2:])
elif choice == '2':
b = input("Enter binary: ")
print("Decimal:", int(b, 2))
else:
print("Invalid choice")
Remove punctuation from string:
import string
s = input("Enter text: ")
print(''.join(ch for ch in s if ch not in string.punctuation))