0% found this document useful (0 votes)
1 views13 pages

Tcs Python Solutions

The document contains a list of 50 coding questions along with their Python 3 solutions, prepared by Abhishek Rathor. Each question covers fundamental programming concepts such as checking even or odd numbers, prime numbers, sorting algorithms, and string manipulations. The solutions are provided in a straightforward manner, suitable for practice or reference for coding interviews.

Uploaded by

godlinashika16
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)
1 views13 pages

Tcs Python Solutions

The document contains a list of 50 coding questions along with their Python 3 solutions, prepared by Abhishek Rathor. Each question covers fundamental programming concepts such as checking even or odd numbers, prime numbers, sorting algorithms, and string manipulations. The solutions are provided in a straightforward manner, suitable for practice or reference for coding interviews.

Uploaded by

godlinashika16
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

TCS NQT 2026 - Top 50 Coding Questions

Python 3 Solutions | Prepared By: Abhishek Rathor (Instagram: @code.abhii07)

1. Check Even or Odd


n = int(input())
print("Even" if n % 2 == 0 else "Odd")

2. Check Prime Number


n = int(input())
prime = True
if n <= 1:
prime = False
else:
i = 2
while i * i <= n:
if n % i == 0:
prime = False
break
i += 1
print("Prime" if prime else "Not Prime")

3. Factorial of a Number
n = int(input())
fact = 1
for i in range(1, n + 1):
fact *= i
print(fact)

4. Fibonacci Series (First N Terms)


n = int(input())
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
5. Reverse a Number
n = int(input())
rev = 0
while n != 0:
rev = rev * 10 + n % 10
n //= 10
print(rev)

6. Check Palindrome Number


n = int(input())
temp, rev = n, 0
while n != 0:
rev = rev * 10 + n % 10
n //= 10
print("Palindrome" if temp == rev else "Not Palindrome")

7. Armstrong Number
n = int(input())
temp, total = n, 0
while n != 0:
digit = n % 10
total += digit ** 3
n //= 10
print("Armstrong" if temp == total else "Not Armstrong")

8. Sum of Digits
n = int(input())
total = 0
while n != 0:
total += n % 10
n //= 10
print(total)
9. Largest of Three Numbers
a, b, c = int(input()), int(input()), int(input())
print(max(a, b, c))

10. GCD of Two Numbers


a, b = int(input()), int(input())
while b != 0:
a, b = b, a % b
print(a)

11. LCM of Two Numbers


a, b = int(input()), int(input())
x, y = a, b
while y != 0:
x, y = y, x % y
gcd = x
lcm = (a * b) // gcd
print(lcm)

12. Check Leap Year


year = int(input())
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap Year")
else:
print("Not Leap Year")
13. Count Vowels and Consonants
s = input().lower()
vowels = consonants = 0
for ch in s:
if [Link]():
if ch in "aeiou":
vowels += 1
else:
consonants += 1
print("Vowels:", vowels)
print("Consonants:", consonants)

14. Reverse a String


s = input()
print(s[::-1])

15. Check Anagram


s1 = input()
s2 = input()
print("Anagram" if sorted(s1) == sorted(s2) else "Not Anagram")

16. Remove Duplicates from String


s = input()
result = ""
for ch in s:
if ch not in result:
result += ch
print(result)
17. Find Second Largest in Array
n = int(input())
arr = [int(input()) for _ in range(n)]
first = second = float("-inf")
for num in arr:
if num > first:
second = first
first = num
elif num > second and num != first:
second = num
print(second)

18. Linear Search


n = int(input())
arr = [int(input()) for _ in range(n)]
key = int(input())
found = False
for num in arr:
if num == key:
found = True
break
print("Found" if found else "Not Found")

19. Binary Search (Sorted Array)


n = int(input())
arr = [int(input()) for _ in range(n)]
key = int(input())
low, high = 0, n - 1
found = False
while low <= high:
mid = (low + high) // 2
if arr[mid] == key:
found = True
break
elif arr[mid] < key:
low = mid + 1
else:
high = mid - 1
print("Found" if found else "Not Found")

20. Bubble Sort


n = int(input())
arr = [int(input()) for _ in range(n)]
for i in range(n - 1):
for j in range(n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
print(*arr)
21. Selection Sort
n = int(input())
arr = [int(input()) for _ in range(n)]
for i in range(n - 1):
min_index = i
for j in range(i + 1, n):
if arr[j] < arr[min_index]:
min_index = j
arr[i], arr[min_index] = arr[min_index], arr[i]
print(*arr)

22. Insertion Sort


n = int(input())
arr = [int(input()) for _ in range(n)]
for i in range(1, n):
key = arr[i]
j = i - 1
while j >= 0 and arr[j] > key:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
print(*arr)

23. Matrix Addition


r, c = int(input()), int(input())
a = [[int(input()) for _ in range(c)] for _ in range(r)]
b = [[int(input()) for _ in range(c)] for _ in range(r)]
for i in range(r):
row = []
for j in range(c):
[Link](a[i][j] + b[i][j])
print(*row)

24. Transpose of Matrix


r, c = int(input()), int(input())
a = [[int(input()) for _ in range(c)] for _ in range(r)]
for j in range(c):
row = [a[i][j] for i in range(r)]
print(*row)
25. Count Frequency of Element in Array
n = int(input())
arr = [int(input()) for _ in range(n)]
key = int(input())
print([Link](key))

26. Check if Array is Sorted


n = int(input())
arr = [int(input()) for _ in range(n)]
sorted_flag = True
for i in range(n - 1):
if arr[i] > arr[i + 1]:
sorted_flag = False
break
print("Sorted" if sorted_flag else "Not Sorted")

27. Merge Two Arrays


n1 = int(input())
a = [int(input()) for _ in range(n1)]
n2 = int(input())
b = [int(input()) for _ in range(n2)]
merged = a + b
print(*merged)

28. Find Missing Number (1 to N)


n = int(input())
arr = [int(input()) for _ in range(n - 1)]
total = n * (n + 1) // 2
print(total - sum(arr))
29. Count Words in a String
s = input().strip()
print(0 if s == "" else len([Link]()))

30. Remove All Spaces from String


s = input()
print([Link](" ", ""))

31. Find Duplicate Elements in Array


n = int(input())
arr = [int(input()) for _ in range(n)]
result = []
for i in range(n):
for j in range(i + 1, n):
if arr[i] == arr[j]:
[Link](arr[i])
break
print(*result)

32. Move All Zeros to End


n = int(input())
arr = [int(input()) for _ in range(n)]
index = 0
for i in range(n):
if arr[i] != 0:
arr[index] = arr[i]
index += 1
while index < n:
arr[index] = 0
index += 1
print(*arr)
33. Rotate Array Right by 1 Position
n = int(input())
arr = [int(input()) for _ in range(n)]
last = arr[-1]
for i in range(n - 1, 0, -1):
arr[i] = arr[i - 1]
arr[0] = last
print(*arr)

34. Check Palindrome String


s = input()
print("Palindrome" if s == s[::-1] else "Not Palindrome")

35. Count Number of Digits


n = int(input())
count = 0
while n != 0:
n //= 10
count += 1
print(count)

36. Sum of Elements in Array


n = int(input())
arr = [int(input()) for _ in range(n)]
print(sum(arr))
37. Find Minimum Element in Array
n = int(input())
arr = [int(input()) for _ in range(n)]
print(min(arr))

38. Pattern Printing (Right Triangle)


n = int(input())
for i in range(1, n + 1):
print("*" * i)

39. Power of a Number


base = int(input())
exp = int(input())
result = 1
for _ in range(exp):
result *= base
print(result)

40. Decimal to Binary


n = int(input())
binary = ""
while n > 0:
binary = str(n % 2) + binary
n //= 2
print(binary)
41. Binary to Decimal
binary = input()
decimal = 0
power = 0
for ch in reversed(binary):
if ch == "1":
decimal += 2 ** power
power += 1
print(decimal)

42. Check Perfect Number


n = int(input())
total = 0
for i in range(1, n // 2 + 1):
if n % i == 0:
total += i
print("Perfect" if total == n else "Not Perfect")

43. Strong Number


def factorial(n):
fact = 1
for i in range(1, n + 1):
fact *= i
return fact

n = int(input())
temp, total = n, 0
while n != 0:
digit = n % 10
total += factorial(digit)
n //= 10
print("Strong" if temp == total else "Not Strong")

44. Count Even and Odd Numbers in Array


n = int(input())
arr = [int(input()) for _ in range(n)]
even = sum(1 for num in arr if num % 2 == 0)
odd = n - even
print("Even:", even)
print("Odd:", odd)
45. Find Intersection of Two Arrays
n1 = int(input())
a = [int(input()) for _ in range(n1)]
n2 = int(input())
b = [int(input()) for _ in range(n2)]
result = []
for x in a:
if x in b:
[Link](x)
print(*result)

46. Check Substring


s = input()
sub = input()
print("Substring Present" if sub in s else "Substring Not Present")

47. Remove Specific Character from String


s = input()
ch = input()[0]
s = [Link](ch, "")
print(s)

48. Sum of Prime Numbers up to N


def is_prime(n):
if n <= 1:
return False
i = 2
while i * i <= n:
if n % i == 0:
return False
i += 1
return True

n = int(input())
total = sum(i for i in range(2, n + 1) if is_prime(i))
print(total)
49. Reverse Words in a Sentence
s = input()
words = [Link](" ")
print(" ".join(reversed(words)))

50. Two Sum Problem


n = int(input())
arr = [int(input()) for _ in range(n)]
target = int(input())
found = False
for i in range(n):
for j in range(i + 1, n):
if arr[i] + arr[j] == target:
print(i, j)
found = True
break
if found:
break
if not found:
print("No Pair Found")

You might also like