1. Reverse a String:
Question: Write a program to reverse a given string without using any built-in reverse function.
Method 1: Using a Loop
You can reverse a string by iterating through it in reverse order and appending each character to a new string.
public static string ReverseString(string input)
{
char[] charArray = input.ToCharArray();
int start = 0;
int end = input.Length - 1;
while (start < end)
{
// Swap characters at start and end positions
char temp = charArray[start];
charArray[start] = charArray[end];
charArray[end] = temp;
start++;
end--;
}
return new string(charArray);
}
// Example usage:
string original = "Hello, World!";
string reversed = ReverseString(original);
Console.WriteLine(reversed); // Outputs: "!dlroW ,olleH"
Method 2: Using LINQ
You can use LINQ to reverse a string by converting it to an array of characters, reversing the array, and then converting it back to a string.
using System;
using System.Linq;
public static string ReverseString(string input)
{
char[] charArray = input.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
// Example usage:
string original = "Hello, World!";
string reversed = ReverseString(original);
Console.WriteLine(reversed); // Outputs: "!dlroW ,olleH"
Method 3: Using Recursion
You can also reverse a string using recursion by recursively reversing a substring.
public static string ReverseString(string input)
{
if (string.IsNullOrEmpty(input))
{
return input;
}
else
{
return ReverseString(input.Substring(1)) + input[0];
}
}
// Example usage:
string original = "Hello, World!";
string reversed = ReverseString(original);
Console.WriteLine(reversed); // Outputs: "!dlroW ,olleH"
2. Check for Palindrome:
Question: Write a program to check if a given string is a palindrome.
Method 1: Using a Loop
You can check if a string is a palindrome by comparing characters from both ends of the string.
public static bool IsPalindrome(string input)
{
int start = 0;
int end = input.Length - 1;
while (start < end)
{
if (input[start] != input[end])
{
return false;
}
start++;
end--;
}
return true;
}
// Example usage:
string palindrome1 = "racecar";
string palindrome2 = "madam";
string notPalindrome = "hello";
Console.WriteLine(IsPalindrome(palindrome1)); // Outputs: True
Console.WriteLine(IsPalindrome(palindrome2)); // Outputs: True
Console.WriteLine(IsPalindrome(notPalindrome)); // Outputs: False
Method 2: Using LINQ
You can use LINQ to reverse the string and then compare it with the original string.
using System;
using System.Linq;
public static bool IsPalindrome(string input)
{
string reversed = new string(input.Reverse().ToArray());
return input == reversed;
}
// Example usage:
string palindrome1 = "racecar";
string palindrome2 = "madam";
string notPalindrome = "hello";
Console.WriteLine(IsPalindrome(palindrome1)); // Outputs: True
Console.WriteLine(IsPalindrome(palindrome2)); // Outputs: True
Console.WriteLine(IsPalindrome(notPalindrome)); // Outputs: False
Method 3: Using Recursion
You can check if a string is a palindrome using recursion by comparing the first and last characters and then recursively checking the substring.
public static bool IsPalindrome(string input)
{
if (input.Length <= 1)
{
return true;
}
else if (input[0] != input[input.Length - 1])
{
return false;
}
else
{
return IsPalindrome(input.Substring(1, input.Length - 2));
}
}
// Example usage:
string palindrome1 = "racecar";
string palindrome2 = "madam";
string notPalindrome = "hello";
Console.WriteLine(IsPalindrome(palindrome1)); // Outputs: True
Console.WriteLine(IsPalindrome(palindrome2)); // Outputs: True
Console.WriteLine(IsPalindrome(notPalindrome)); // Outputs: False
These methods provide different ways to check if a string is a palindrome in C#. You can choose the one that best fits your coding style and requirements.
3. Factorial Calculation:
Question: Write a program to calculate the factorial of a given number.
Method 1: Using a Loop
You can calculate the factorial of a number using a loop by multiplying numbers from 1 to n.
public static int CalculateFactorial(int n)
{
if (n < 0)
{
throw new ArgumentException("Factorial is not defined for negative numbers.");
}
int result = 1;
for (int i = 1; i <= n; i++)
{
result *= i;
}
return result;
}
// Example usage:
int number1 = 5;
int number2 = 0;
Console.WriteLine(CalculateFactorial(number1)); // Outputs: 120 (5!)
Console.WriteLine(CalculateFactorial(number2)); // Outputs: 1 (0! is defined as 1)
Method 2: Using Recursion
You can calculate the factorial of a number using recursion by defining the factorial of 0 as 1 and recursively calculating the factorial for positive integers.
public static int CalculateFactorial(int n)
{
if (n < 0)
{
throw new ArgumentException("Factorial is not defined for negative numbers.");
}
if (n == 0)
{
return 1;
}
else
{
return n * CalculateFactorial(n - 1);
}
}
// Example usage:
int number1 = 5;
int number2 = 0;
Console.WriteLine(CalculateFactorial(number1)); // Outputs: 120 (5!)
Console.WriteLine(CalculateFactorial(number2)); // Outputs: 1 (0! is defined as 1)
Method 3: Using a While Loop
You can calculate the factorial of a number using a while loop similarly to the first method.
public static int CalculateFactorial(int n)
{
if (n < 0)
{
throw new ArgumentException("Factorial is not defined for negative numbers.");
}
int result = 1;
int i = 1;
while (i <= n)
{
result *= i;
i++;
}
return result;
}
// Example usage:
int number1 = 5;
int number2 = 0;
Console.WriteLine(CalculateFactorial(number1)); // Outputs: 120 (5!)
Console.WriteLine(CalculateFactorial(number2)); // Outputs: 1 (0! is defined as 1)
These methods provide different ways to calculate the factorial of a number in C#. You can choose the one that best fits your coding style and requirements.
4. Fibonacci Series:
Question: Write a program to generate the Fibonacci series up to a given number of terms.
Method 1: Using a Loop
You can generate the Fibonacci series using a loop by starting with the first two Fibonacci numbers (0 and 1) and then iteratively calculating the next numbers in the series.
public static List GenerateFibonacciSeries(int n)
{
List series = new List();
int a = 0, b = 1;
for (int i = 0; i < n; i++)
{
series.Add(a);
int temp = a;
a = b;
b = temp + b;
}
return series;
}
// Example usage:
int count = 10;
List fibonacciSeries = GenerateFibonacciSeries(count);
Console.WriteLine(string.Join(", ", fibonacciSeries)); // Outputs: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
Method 2: Using Recursion
You can generate the Fibonacci series using recursion by defining the first two Fibonacci numbers and recursively calculating the next numbers in the series.
public static int Fibonacci(int n)
{
if (n <= 1)
{
return n;
}
else
{
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
}
public static List GenerateFibonacciSeries(int n)
{
List series = new List();
for (int i = 0; i < n; i++)
{
series.Add(Fibonacci(i));
}
return series;
}
// Example usage:
int count = 10;
List fibonacciSeries = GenerateFibonacciSeries(count);
Console.WriteLine(string.Join(", ", fibonacciSeries)); // Outputs: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
Method 3: Using an Array
You can generate the Fibonacci series using an array to store previously calculated values.
public static List GenerateFibonacciSeries(int n)
{
List series = new List();
int[] fib = new int[n];
for (int i = 0; i < n; i++)
{
if (i <= 1)
{
fib[i] = i;
}
else
{
fib[i] = fib[i - 1] + fib[i - 2];
}
series.Add(fib[i]);
}
return series;
}
// Example usage:
int count = 10;
List fibonacciSeries = GenerateFibonacciSeries(count);
Console.WriteLine(string.Join(", ", fibonacciSeries)); // Outputs: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
These methods provide different ways to generate the Fibonacci series in C#. You can choose the one that best fits your coding style and requirements.
5. Check for Prime Numbers:
Question: Write a program to check if a given number is prime.
Method 1: Basic Loop
You can check if a number is prime by iterating through numbers from 2 to the square root of the number and checking for divisibility.
public static bool IsPrime(int number)
{
if (number <= 1)
{
return false;
}
for (int i = 2; i * i <= number; i++)
{
if (number % i == 0)
{
return false;
}
}
return true;
}
// Example usage:
int num1 = 17; // Prime number
int num2 = 10; // Not a prime number
Console.WriteLine(IsPrime(num1)); // Outputs: True
Console.WriteLine(IsPrime(num2)); // Outputs: False
Method 2: Sieve of Eratosthenes
You can use the Sieve of Eratosthenes algorithm to generate prime numbers up to a certain limit and then check if the number is in the list of generated primes.
public static List GeneratePrimes(int n)
{
bool[] isPrime = new bool[n + 1];
List primes = new List();
for (int i = 2; i <= n; i++)
{
isPrime[i] = true;
}
for (int p = 2; p * p <= n; p++)
{
if (isPrime[p])
{
for (int i = p * p; i <= n; i += p)
{
isPrime[i] = false;
}
}
}
for (int i = 2; i <= n; i++)
{
if (isPrime[i])
{
primes.Add(i);
}
}
return primes;
}
public static bool IsPrime(int number)
{
if (number <= 1)
{
return false;
}
List primes = GeneratePrimes((int)Math.Sqrt(number));
foreach (int prime in primes)
{
if (number % prime == 0)
{
return false;
}
}
return true;
}
// Example usage:
int num1 = 17; // Prime number
int num2 = 10; // Not a prime number
Console.WriteLine(IsPrime(num1)); // Outputs: True
Console.WriteLine(IsPrime(num2)); // Outputs: False
These methods provide different ways to check if a number is prime in C#. You can choose the one that best fits your coding style and requirements.
0 Comments