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

csharp_questions_that_has_been_asking

The document provides a series of C# programming exercises, including reversing a string, reversing words in a sentence, checking for palindromes, calculating factorials using recursion, generating Fibonacci sequences, finding missing numbers in arrays, identifying non-repeating characters, finding intersections of arrays, checking for anagrams, and removing duplicates from arrays. Each exercise includes a question, a solution in C#, and the expected output. These examples serve as practical coding challenges for learning and practicing C# programming.

Uploaded by

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

csharp_questions_that_has_been_asking

The document provides a series of C# programming exercises, including reversing a string, reversing words in a sentence, checking for palindromes, calculating factorials using recursion, generating Fibonacci sequences, finding missing numbers in arrays, identifying non-repeating characters, finding intersections of arrays, checking for anagrams, and removing duplicates from arrays. Each exercise includes a question, a solution in C#, and the expected output. These examples serve as practical coding challenges for learning and practicing C# programming.

Uploaded by

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

1.

Reverse a String in C#
Question: Write a C# program to reverse a given string.

Solution:
csharp
Copy
using System;

class Program
{
static void Main()
{
string input = "i like programming very much";
char[] charArray = input.ToCharArray();
Array.Reverse(charArray);
string reversed = new string(charArray);
Console.WriteLine(reversed);
}
}
Output:

css
Copy
hcum yrev gnimmargorp ekil i
2. Reverse Words in a Sentence
Question: Given a sentence, reverse the order of words.

Solution:
csharp
Copy
using System;
using System.Linq;

class Program
{
static void Main()
{
string input = "i like programming very much";
string reversedWords = string.Join(" ", input.Split(' ').Reverse());
Console.WriteLine(reversedWords);
}
}
Output:

css
Copy
much very programming like i
3. Check If a String Is a Palindrome
Question: Write a C# program to check if a given string is a palindrome.

Solution:
csharp
Copy
using System;

class Program
{
static bool IsPalindrome(string str)
{
str = str.ToLower().Replace(" ", ""); // Convert to lowercase and remove
spaces
char[] charArray = str.ToCharArray();
Array.Reverse(charArray);
return str == new string(charArray);
}

static void Main()


{
Console.WriteLine(IsPalindrome("madam")); // True
Console.WriteLine(IsPalindrome("hello")); // False
}
}
Output:

graphql
Copy
True
False
4. Find the Factorial of a Number Using Recursion
Question: Write a program to compute the factorial of a number using recursion.

Solution:
csharp
Copy
using System;

class Program
{
static int Factorial(int n)
{
return (n == 0) ? 1 : n * Factorial(n - 1);
}

static void Main()


{
Console.WriteLine(Factorial(5)); // 120
}
}
Output:

Copy
120
5. Find the Fibonacci Series up to N Terms
Question: Write a C# program to print the Fibonacci sequence up to N terms.

Solution:
csharp
Copy
using System;

class Program
{
static void Fibonacci(int n)
{
int a = 0, b = 1, temp;
for (int i = 0; i < n; i++)
{
Console.Write(a + " ");
temp = a + b;
a = b;
b = temp;
}
}

static void Main()


{
Fibonacci(10); // Output: 0 1 1 2 3 5 8 13 21 34
}
}
6. Find the Missing Number in an Array
Question: Given an array containing n numbers from 1 to n+1 with one missing, find
the missing number.

Solution:
csharp
Copy
using System;
using System.Linq;

class Program
{
static int FindMissingNumber(int[] nums)
{
int n = nums.Length + 1;
int totalSum = (n * (n + 1)) / 2; // Sum of first n natural numbers
int arraySum = nums.Sum();
return totalSum - arraySum;
}

static void Main()


{
int[] nums = {1, 2, 4, 5, 6};
Console.WriteLine(FindMissingNumber(nums)); // Output: 3
}
}
7. Find the First Non-Repeating Character in a String
Question: Write a program to find the first non-repeating character in a string.

Solution:
csharp
Copy
using System;
using System.Collections.Generic;

class Program
{
static char FirstNonRepeatingCharacter(string str)
{
Dictionary<char, int> charCount = new Dictionary<char, int>();

foreach (char ch in str)


{
if (charCount.ContainsKey(ch))
charCount[ch]++;
else
charCount[ch] = 1;
}
foreach (char ch in str)
{
if (charCount[ch] == 1)
return ch;
}

return '_'; // No unique character found


}

static void Main()


{
Console.WriteLine(FirstNonRepeatingCharacter("swiss")); // Output: 'w'
}
}
8. Find the Intersection of Two Arrays
Question: Given two arrays, find the common elements between them.

Solution:
csharp
Copy
using System;
using System.Linq;

class Program
{
static void Main()
{
int[] arr1 = {1, 2, 3, 4, 5};
int[] arr2 = {3, 4, 5, 6, 7};

int[] intersection = arr1.Intersect(arr2).ToArray();


Console.WriteLine(string.Join(", ", intersection)); // Output: 3, 4, 5
}
}
9. Check If Two Strings Are Anagrams
Question: Write a program to check if two strings are anagrams.

Solution:
csharp
Copy
using System;
using System.Linq;

class Program
{
static bool AreAnagrams(string str1, string str2)
{
return str1.OrderBy(c => c).SequenceEqual(str2.OrderBy(c => c));
}

static void Main()


{
Console.WriteLine(AreAnagrams("listen", "silent")); // True
Console.WriteLine(AreAnagrams("hello", "world")); // False
}
}
10. Remove Duplicates from an Array
Question: Write a program to remove duplicate elements from an array.
Solution:
csharp
Copy
using System;
using System.Linq;

class Program
{
static void Main()
{
int[] arr = {1, 2, 2, 3, 4, 4, 5};
int[] uniqueArr = arr.Distinct().ToArray();
Console.WriteLine(string.Join(", ", uniqueArr)); // Output: 1, 2, 3, 4, 5
}
}

You might also like