I would be very happy if someone will provide better solution for these questions -
Question 1: Exchange Two integer variable value without using third variable.
Answer:
- public static void ExchangeInteger()
- {
- int a = 10;
- int b = 5;
- Console.WriteLine("a={0}", a);
- Console.WriteLine("b={0}", b);
- a = a + b;
- b = a - b;
- a = a - b;
- Console.WriteLine("a={0}", a);
- Console.WriteLine("b={0}", b);
- Console.ReadLine();
- }
Calling above method,
- static void Main(string[] args)
- {
- clsExchangevariable.ExchangeInteger();
- }
Output
Question 2: Swap neighbor char in string, For Example string “TAPAN” would be:
Answer:
- public static string SwapNeighbourChar(string strToSwap)
- {
- char[] arraStr = strToSwap.ToCharArray();
- StringBuilder strbuild = new StringBuilder();
- for (int i = 0; i <= arraStr.Length - 1; i++)
- {
- if (i != arraStr.Length - 1)
- {
- strbuild.Append(arraStr[i + 1]);
- }
- strbuild.Append(arraStr[i]);
- i = i + 1;
- }
- return strbuild.ToString();
- }
Calling above method
- static void Main(string[] args)
- {
- Console.WriteLine(clsSwapNeighbour.SwapNeighbourChar("TAPAN"));
- Console.ReadLine();
- }
Output
Question 3: Is prime number?
Answer:
- public static bool IsPrimeNumbers(int number)
- {
- bool returnMsg = false;
- for (int i = 2; i <= number; i++)
- {
- if ((number % i) == 0 && number != i)
- {
- returnMsg = false;
- return returnMsg;
- }
- else if (number == i)
- {
- returnMsg = true;
- return returnMsg;
- }
- }
- return returnMsg;
- }
Calling above method,
- static void Main(string[] args)
- {
- string message = clsIsPrimeNumber.IsPrimeNumbers(17) == true ? "Prime Number" : "Not a prime number";
- Console.WriteLine(message);
- message = clsIsPrimeNumber.IsPrimeNumbers(4) == true ? "Prime Number" : "Not a prime number";
- Console.WriteLine(message);
- message = clsIsPrimeNumber.IsPrimeNumbers(11) == true ? "Prime Number" : "Not a prime number";
- Console.WriteLine(message);
- message = clsIsPrimeNumber.IsPrimeNumbers(21) == true ? "Prime Number" : "Not a prime number";
- Console.WriteLine(message);
- Console.ReadLine();
- }
Output:
Question 4: Fibonacci series.
Answer:
- public static void PrintFibonacciSeries(int limit)
- {
- int digit1, digit2, digit3;
- digit1 = 0;
- digit2 = 1;
- digit3 = digit1 + digit2;
- Console.WriteLine(digit1);
- Console.WriteLine(digit2);
- Console.WriteLine(digit3);
- for (int i = 0; i < limit; i++)
- {
- digit1 = digit2;
- digit2 = digit3;
- digit3 = digit1 + digit2;
- Console.WriteLine(digit3);
- }
- }
Calling above method,
- static void Main(string[] args)
- {
- clsFibonacci.PrintFibonacciSeries(10);
- Console.ReadLine();
- }
Output:
Question 5: Get Factorial of number.
Answer:
- public static int PrintFactorialOfNumber(int number)
- {
-
- int result = 1;
- for (int i = number; i > 0; i--)
- {
- result = result * i;
- }
- return result;
- }
Calling above method:
- static void Main(string[] args)
- {
- int factorial;
- factorial = clsfactorial.PrintFactorialOfNumber(5);
- Console.WriteLine("Factorial Of 5 Is " + factorial.ToString());
- factorial = clsfactorial.PrintFactorialOfNumber(7);
- Console.WriteLine("Factorial Of 7 Is " + factorial.ToString());
- Console.ReadLine();
- }
Output: