
Practice 100 C# Basic Algorithm Programming Questions
Q1. Write a C# program to find the largest of three numbers.
Input: a = 10, b = 20, c = 15
Output: 20
Q2. Write a C# program to calculate the factorial of a number using recursion.
Input: n = 5
Output: 120
Q3. Write a C# program to calculate the Fibonacci sequence up to n terms.
Input: n = 5
Output: 0, 1, 1, 2, 3
Q4. Write a C# program to check if a number is a palindrome.
Input: 121
Output: Palindrome
Q5. Write a C# program to implement binary search on a sorted array.
Input: Array = [1, 3, 5, 7, 9], Target = 5
Output: Index: 2
Q6. Write a C# program to find the greatest common divisor (GCD) of two numbers.
Input: a = 56, b = 98
Output: 14
Q7. Write a C# program to find the least common multiple (LCM) of two numbers.
Input: a = 4, b = 6
Output: 12
Q8. Write a C# program to reverse the digits of a number.
Input: 1234
Output: 4321
Q9. Write a C# program to find the sum of n natural numbers.
Input: n = 10
Output: 55
Q10. Write a C# program to find the product of n natural numbers.
Input: n = 5
Output: 120
Q11. Write a C# program to calculate the power of a number (x^y) without using Math.Pow().
Input: x = 2, y = 3
Output: 8
Q12. Write a C# program to print all prime factors of a number.
Input: n = 28
Output: 2, 7
Q13. Write a C# program to check if a number is a perfect square.
Input: n = 16
Output: True
Q14. Write a C# program to generate all subsets of a set.
Input: Set = [1, 2, 3]
Output:
[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]
Q15. Write a C# program to count the number of inversions in an array.
Input: [2, 4, 1, 3, 5]
Output: 3
Q16. Write a C# program to implement the Sieve of Eratosthenes to find all prime numbers up to n.
Input: n = 10
Output: 2, 3, 5, 7
Q17. Write a C# program to find the longest common prefix of an array of strings.
Input: ["flower", "flow", "flight"]
Output: "fl"
Q18. Write a C# program to perform insertion sort on an array.
Input: [3, 1, 4, 2]
Output: [1, 2, 3, 4]
Q19. Write a C# program to perform selection sort on an array.
Input: [3, 1, 4, 2]
Output: [1, 2, 3, 4]
Q20. Write a C# program to perform bubble sort on an array.
Input: [3, 1, 4, 2]
Output: [1, 2, 3, 4]
Q21. Write a C# program to perform merge sort on an array.
Input: [38, 27, 43, 3, 9, 82, 10]
Output: [3, 9, 10, 27, 38, 43, 82]
Q22. Write a C# program to perform quicksort on an array.
Input: [10, 7, 8, 9, 1, 5]
Output: [1, 5, 7, 8, 9, 10]
Q23. Write a C# program to find the minimum and maximum element in an array.
Input: [3, 5, 1, 9, 2]
Output: Min: 1, Max: 9
Q24. Write a C# program to find the missing number in an array of 1 to n.
Input: [1, 2, 4, 5]
Output: 3
Q25. Write a C# program to find the first non-repeating character in a string.
Input: swiss
Output: w
Q26. Write a C# program to remove all duplicates from a sorted array.
Input: [1, 1, 2, 2, 3, 4, 4]
Output: [1, 2, 3, 4]
Q27. Write a C# program to find the kth smallest element in an array.
Input: Array = [7, 10, 4, 3, 20, 15], k = 3
Output: 7
Q28. Write a C# program to sort an array of 0s, 1s, and 2s.
Input: [2, 0, 2, 1, 1, 0]
Output: [0, 0, 1, 1, 2, 2]
Q29. Write a C# program to find the maximum sum subarray using Kadane’s Algorithm.
Input: [-2, 1, -3, 4, -1, 2, 1, -5, 4]
Output: 6
Q30. Write a C# program to check if a string is a valid palindrome, ignoring non-alphanumeric characters.
Input: "A man, a plan, a canal: Panama"
Output: True
Q31. Write a C# program to merge two sorted arrays into a single sorted array.
Input: Array1 = [1, 3, 5], Array2 = [2, 4, 6]
Output: [1, 2, 3, 4, 5, 6]
Q32. Write a C# program to calculate the nth Fibonacci number using dynamic programming.
Input: n = 10
Output: 55
Q33. Write a C# program to count the number of bits set to 1 in the binary representation of a number.
Input: n = 9
Output: 2
Q34. Write a C# program to rotate an array to the left by k steps.
Input: Array = [1, 2, 3, 4, 5], k = 2
Output: [3, 4, 5, 1, 2]
Q35. Write a C# program to find the intersection of two arrays using a hash set.
Input: Array1 = [1, 2, 2, 1], Array2 = [2, 2]
Output: [2]
Q36. Write a C# program to implement the Two Sum algorithm.
Input: Array = [2, 7, 11, 15], Target = 9
Output: Indices: [0, 1]
Q37. Write a C# program to find the maximum product of two elements in an array.
Input: [1, 5, 4, 5]
Output: 16
Q38. Write a C# program to find the smallest number greater than n in an array.
Input: Array = [1, 3, 5, 7], n = 4
Output: 5
Q39. Write a C# program to check if two strings are permutations of each other.
Input: str1 = "abc", str2 = "bca"
Output: True
Q40. Write a C# program to find the equilibrium index of an array.
Input: [1, 3, 5, 2, 2]
Output: 2
Q41. Write a C# program to count the number of vowels and consonants in a string.
Input: "hello"
Output: Vowels: 2, Consonants: 3
Q42. Write a C# program to find the second largest element in an array.
Input: [10, 20, 4, 45, 99]
Output: 45
Q43. Write a C# program to remove duplicate elements from an unsorted array.
Input: [4, 5, 9, 4, 9, 7, 6]
Output: [4, 5, 9, 7, 6]
Q44. Write a C# program to count the number of occurrences of each element in an array.
Input: [2, 3, 2, 4, 2, 4, 3]
Output:
2: 3
3: 2
4: 2
Q45. Write a C# program to find the longest palindrome substring in a string.
Input: "babad"
Output: "bab" or "aba"
Q46. Write a C# program to implement linear search in an array.
Input: Array = [1, 3, 5, 7, 9], Target = 7
Output: Index: 3
Q47. Write a C# program to calculate the sum of all even numbers and odd numbers separately in an array.
Input: [1, 2, 3, 4, 5, 6]
Output: Even Sum: 12, Odd Sum: 9
Q48. Write a C# program to check if a given string is an anagram of another string.
Input: str1 = "listen", str2 = "silent"
Output: True
Q49. Write a C# program to find the sum of all elements in a 2D array.
Input:
[[1, 2],
[3, 4]]
Output: 10
Q50. Write a C# program to implement a basic Caesar Cipher encryption.
Input: Text: "hello", Shift: 3
Output: "khoor"
Q51. Write a C# program to check if an array is sorted in ascending order.
Input: [1, 2, 3, 4, 5]
Output: True
Q52. Write a C# program to find the largest product of any three numbers in an array.
Input: [1, 10, -5, 1, -100]
Output: 5000
Q53. Write a C# program to calculate the sum of the boundary elements in a matrix.
Input:
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
Output: 40
Q54. Write a C# program to reverse a string without using inbuilt functions.
Input: "hello"
Output: "olleh"
Q55. Write a C# program to calculate the average of all numbers in an array.
Input: [1, 2, 3, 4, 5]
Output: 3
Q56. Write a C# program to find all pairs of numbers in an array that sum to a given value.
Input: Array = [1, 5, 7, -1], Sum = 6
Output: Pairs: (1, 5), (7, -1)
Q57. Write a C# program to remove all vowels from a string.
Input: "beautiful"
Output: "btfl"
Q58. Write a C# program to check if a given number is an Armstrong number.
Input: 153
Output: True
Q59. Write a C# program to find the length of the longest word in a sentence.
Input: "I love programming"
Output: 11
Q60. Write a C# program to calculate the Hamming distance between two strings.
Input: str1 = "karolin", str2 = "kathrin"
Output: 3
Q61. Write a C# program to check if a number is a happy number.
Input: 19
Output: True
Q62. Write a C# program to find the first repeated character in a string.
Input: "programming"
Output: r
Q63. Write a C# program to find the sum of the digits of a number until a single digit is obtained.
Input: 9875
Output: 2
Q64. Write a C# program to check if two strings are rotations of each other.
Input: str1 = "abcd", str2 = "cdab"
Output: True
Q65. Write a C# program to count the number of words in a sentence.
Input: "Hello, world!"
Output: 2
Q66. Write a C# program to reverse the words in a sentence.
Input: "I love C#"
Output: "C# love I"
Q67. Write a C# program to calculate the product of all elements in an array.
Input: [1, 2, 3, 4]
Output: 24
Q68. Write a C# program to implement the Tower of Hanoi algorithm for n disks.
Input: n = 3
Output:
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C
Q69. Write a C# program to generate Pascal's Triangle for n rows.
Input: n = 5
Output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Q70. Write a C# program to find the longest increasing subsequence in an array.
Input: [10, 22, 9, 33, 21, 50, 41, 60, 80]
Output: 6
Q71. Write a C# program to find the transpose of a matrix.
Input:
[[1, 2],
[3, 4]]
Output:
[[1, 3],
[2, 4]]
Q72. Write a C# program to check if a matrix is symmetric.
Input:
[[1, 2, 3],
[2, 4, 5],
[3, 5, 6]]
Output: True
Q73. Write a C# program to find all prime numbers in an interval.
Input: Start = 10, End = 20
Output: 11, 13, 17, 19
Q74. Write a C# program to calculate the sum of diagonal elements of a square matrix.
Input:
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
Output: 15
Q75. Write a C# program to calculate the area of a triangle using Heron's formula.
Input: a = 3, b = 4, c = 5
Output: 6
Q76. Write a C# program to find the missing character in a string to make it a pangram.
Input: "The quick brown fox jumps over the lazy do"
Output: "g"
Q77. Write a C# program to find the longest common subsequence of two strings.
Input: str1 = "abcde", str2 = "ace"
Output: "ace"
Q78. Write a C# program to find the sum of all proper divisors of a number.
Input: 28
Output: 28
Q79. Write a C# program to calculate the digital root of a number.
Input: 12345
Output: 6
Q80. Write a C# program to count the number of trailing zeros in the factorial of a number.
Input: n = 10
Output: 2
Q81. Write a C# program to check if a string has balanced parentheses.
Input: "((a+b)*c)"
Output: True
Q82. Write a C# program to implement binary search in a sorted array.
Input: Array = [1, 3, 5, 7, 9], Target = 5
Output: Index: 2
Q83. Write a C# program to find the GCD (Greatest Common Divisor) of two numbers.
Input: a = 56, b = 98
Output: 14
Q84. Write a C# program to calculate the power of a number using recursion.
Input: Base = 2, Exponent = 5
Output: 32
Q85. Write a C# program to find the sum of the squares of the first n natural numbers.
Input: n = 5
Output: 55
Q86. Write a C# program to generate all permutations of a string.
Input: "abc"
Output: "abc, acb, bac, bca, cab, cba"
Q87. Write a C# program to implement the Sieve of Eratosthenes to find all prime numbers up to n.
Input: n = 30
Output: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
Q88. Write a C# program to find the maximum depth of nested parentheses in a string.
Input: "(a(b(c)d)e)"
Output: 3
Q89. Write a C# program to find the majority element in an array.
Input: [3, 3, 4, 2, 4, 4, 2, 4, 4]
Output: 4
Q90. Write a C# program to count the number of distinct elements in an array.
Input: [1, 2, 2, 3, 4, 4, 5]
Output: 5
Q91. Write a C# program to implement Bubble Sort.
Input: [64, 34, 25, 12, 22, 11, 90]
Output: [11, 12, 22, 25, 34, 64, 90]
Q92. Write a C# program to find the longest substring without repeating characters.
Input: "abcabcbb"
Output: "abc"
Q93. Write a C# program to check if a number is a power of two.
Input: n = 16
Output: True
Q94. Write a C# program to find the first k largest elements in an array.
Input: Array = [1, 23, 12, 9, 30, 2, 50], k = 3
Output: [50, 30, 23]
Q95. Write a C# program to calculate the nth Catalan number.
Input: n = 4
Output: 14
Q96. Write a C# program to implement insertion sort.
Input: [12, 11, 13, 5, 6]
Output: [5, 6, 11, 12, 13]
Q97. Write a C# program to calculate the sum of all prime numbers up to n.
Input: n = 10
Output: 17
Q98. Write a C# program to find the row with the maximum sum in a 2D array.
Input:
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
Output: Row 3, Sum: 24
Q99. Write a C# program to check if a given number is a perfect square.
Input: n = 49
Output: True
Q100. Write a C# program to reverse the order of columns in a 2D array.
Input:
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
Output:
[[3, 2, 1],
[6, 5, 4],
[9, 8, 7]]