C# coding challenges are commonly posed during interviews. These challenges cover a wide range of topics including fundamentals, arrays, strings, control flow, file manipulation, and more. Below is a compilation of popular C# programming exercises.
1) Fibonacci Series
To generate the Fibonacci series in C# without using recursion, we can use a loop to calculate the next Fibonacci number based on the previous two numbers. Here's an example:
// Without using recursion
int n = 10;
int firstNum = 0, secondNum = 1, nextNum;
Console.WriteLine("Fibonacci Series without using recursion:");
for (int i = 0; i < n; i++)
{
if (i <= 1)
{
nextNum = i;
}
else
{
nextNum = firstNum + secondNum;
firstNum = secondNum;
secondNum = nextNum;
}
Console.Write(nextNum + " ");
}
When using recursion to print the Fibonacci series in C#, we define a function that calls itself recursively to calculate the Fibonacci numbers. Here's an example:
// Using recursion
static int Fibonacci(int n)
{
if (n <= 1)
{
return n;
}
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
int count = 10;
Console.WriteLine("\n\nFibonacci Series using recursion:");
for (int i = 0; i < count; i++)
{
Console.Write(Fibonacci(i) + " ");
}
These examples demonstrate how to generate the Fibonacci series in C# both with and without using recursion.
Input: 15
Output: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
2) Prime number
Write a c# program to check prime number.
Input: 17
Output: prime number
Input: 57
Output: not prime number
3) Palindrome number
Write a c# program to check palindrome number.
Input: 121
Output: palindrome number
Input: 113
Output: not palindrome number
4) Factorial
Create a C# program that calculates and displays the factorial of a given number.
Input: 6
Output: 720
5) Armstrong number
Write a c# program to check armstrong number.
Input: 371
Output: armstrong
Input: 342
Output: not armstrong
6) Sum of Digits
Write a c# program to print sum of digits.
Input: 23
Output: 5
Input: 624
Output: 12
7) Reverse Number
Write a c# program to reverse given number.
Input: 234
Output: 432
8) Swap two numbers without using third variable
To interchange the values of two numbers without utilizing a third variable in a C# program, you can employ the XOR bitwise operator. This operator helps in swapping values without the need for an additional variable. Below is an example code snippet demonstrating this technique:
using System;
class Program
{
static void Main()
{
int num1 = 10;
int num2 = 20;
Console.WriteLine($"Before swapping: num1 = {num1}, num2 = {num2}");
num1 = num1 ^ num2;
num2 = num1 ^ num2;
num1 = num1 ^ num2;
Console.WriteLine($"After swapping: num1 = {num1}, num2 = {num2}");
}
}
In this code snippet, the XOR operator is used to swap the values of num1 and num2 without requiring a third variable.
Input: a=5 b=10
Output: a=10 b=5
9) Decimal to Binary
Create a C# program that converts a decimal number to its binary equivalent.
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
Here is an example of how you can achieve this in C#:
using System;
class DecimalToBinaryConverter
{
static void Main()
{
Console.Write("Enter a decimal number: ");
int decimalNumber = int.Parse(Console.ReadLine());
string binaryNumber = ConvertDecimalToBinary(decimalNumber);
Console.WriteLine($"Binary equivalent: {binaryNumber}");
}
static string ConvertDecimalToBinary(int decimalNumber)
{
return Convert.ToString(decimalNumber, 2);
}
}
In this program, the ConvertDecimalToBinary method takes an integer decimal number as input and uses the Convert.ToString method to convert it to a binary string. The binary equivalent is then displayed to the user.
Input: 10
Output: 1010
10) Alphabet Triangle
Write a c# program to print alphabet triangle.
Output:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
11) Number Triangle
Write a c# program to print number triangle.
Input: 5
Output:
enter the range= 6
1
121
2321
1234321
123454321
12345654321
12) Fibonacci Triangle
Create a C# application that generates a Fibonacci triangle.
Input: 9
Output:
1
1 1
1 1 2
1 1 2 3
1 1 2 3 5
1 1 2 3 5 8
1 1 2 3 5 8 13
1 1 2 3 5 8 13 21
1 1 2 3 5 8 13 21 34
13) Number in Characters
Create a C# program that converts a number into its corresponding words.
Input: 357546
Output: three five seven five four six