1. Reverse string without built-in function
string input = "Hello";
string result = "";
for (int i = input.Length - 1; i >= 0; i--)
{
result += input[i];
}
Console.WriteLine(result);
2. Remove duplicate characters from string
string input = "programming";
string output = new string(input.Distinct().ToArray());
Console.WriteLine(output);
3. Count occurrences of each character
string s = "hello";
var result = s.GroupBy(x => x)
.Select(g => new { Char = g.Key, Count = g.Count() });
foreach (var item in result)
Console.WriteLine(item.Char + " = " + item.Count);
4. Find 2nd highest number in array
int[] a = { 4, 8, 1, 9, 3, 7 };
int second = a.OrderByDescending(x => x).Skip(1).First();
Console.WriteLine(second);
5. Check Palindrome
string s = "madam";
bool isPal = s == new string(s.Reverse().ToArray());
Console.WriteLine(isPal);
6. Find Prime numbers in range
for (int i = 2; i <= 50; i++)
{
bool prime = true;
for (int j = 2; j <= Math.Sqrt(i); j++)
if (i % j == 0) { prime = false; break; }
if (prime) Console.Write(i + " ");
}