C# Logical Questions with Answer

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:

  1. public static void ExchangeInteger()  
  2. {  
  3.     int a = 10;  
  4.     int b = 5;  
  5.     Console.WriteLine("a={0}", a);  
  6.     Console.WriteLine("b={0}", b);  
  7.     a = a + b; // Now a would be 15   
  8.     b = a - b; // Now b would be 15-5=10 ........   
  9.     a = a - b; // Now a would be 15-10=5........   
  10.     Console.WriteLine("a={0}", a);  
  11.     Console.WriteLine("b={0}", b);  
  12.     Console.ReadLine();  
  13. }  
Calling above method,
  1. static void Main(string[] args)  
  2. {  
  3.   clsExchangevariable.ExchangeInteger();  
  4. }  
Output

Exchange Two integer variable value

 

Question 2: Swap neighbor char in string, For Example string “TAPAN” would be:

neighbor char in string

Answer:

  1. public static string SwapNeighbourChar(string strToSwap)  
  2. {  
  3.     char[] arraStr = strToSwap.ToCharArray();  
  4.     StringBuilder strbuild = new StringBuilder();  
  5.     for (int i = 0; i <= arraStr.Length - 1; i++)  
  6.     {  
  7.         if (i != arraStr.Length - 1)  
  8.         {  
  9.             strbuild.Append(arraStr[i + 1]);  
  10.         }  
  11.         strbuild.Append(arraStr[i]);  
  12.         i = i + 1;  
  13.     }  
  14.     return strbuild.ToString();  
  15. }  
Calling above method
  1. static void Main(string[] args)  
  2. {  
  3.      Console.WriteLine(clsSwapNeighbour.SwapNeighbourChar("TAPAN"));  
  4.      Console.ReadLine();  
  5. }  
Output

Swap neighbor char in string

Question 3: Is prime number?

Answer:
  1. public static bool IsPrimeNumbers(int number)  
  2. {  
  3.     bool returnMsg = false;  
  4.     for (int i = 2; i <= number; i++)  
  5.     {  
  6.         if ((number % i) == 0 && number != i)  
  7.         {  
  8.             returnMsg = false;  
  9.             return returnMsg;  
  10.         }  
  11.         else if (number == i)  
  12.         {  
  13.             returnMsg = true;  
  14.             return returnMsg;  
  15.         }  
  16.     }  
  17.     return returnMsg;  
  18. }  
Calling above method,
  1. static void Main(string[] args)  
  2. {  
  3.     string message = clsIsPrimeNumber.IsPrimeNumbers(17) == true ? "Prime Number" : "Not a prime number";  
  4.     Console.WriteLine(message);  
  5.     message = clsIsPrimeNumber.IsPrimeNumbers(4) == true ? "Prime Number" : "Not a prime number";  
  6.     Console.WriteLine(message);  
  7.     message = clsIsPrimeNumber.IsPrimeNumbers(11) == true ? "Prime Number" : "Not a prime number";  
  8.     Console.WriteLine(message);  
  9.     message = clsIsPrimeNumber.IsPrimeNumbers(21) == true ? "Prime Number" : "Not a prime number";  
  10.     Console.WriteLine(message);  
  11.     Console.ReadLine();  
  12. }  
Output:

s prime number

Question 4: Fibonacci series.

Answer:
  1. public static void PrintFibonacciSeries(int limit)  
  2. {  
  3.     int digit1, digit2, digit3;  
  4.     digit1 = 0;  
  5.     digit2 = 1;  
  6.     digit3 = digit1 + digit2;  
  7.     Console.WriteLine(digit1);  
  8.     Console.WriteLine(digit2);  
  9.     Console.WriteLine(digit3);  
  10.     for (int i = 0; i < limit; i++)  
  11.     {  
  12.         digit1 = digit2;  
  13.         digit2 = digit3;  
  14.         digit3 = digit1 + digit2;  
  15.         Console.WriteLine(digit3);  
  16.     }  
  17. }  
Calling above method,
  1. static void Main(string[] args)  
  2. {  
  3.   clsFibonacci.PrintFibonacciSeries(10);  
  4.   Console.ReadLine();  
  5. }  
Output:

ibonacci series

Question 5: Get Factorial of number.

Answer:
  1. public static int PrintFactorialOfNumber(int number)  
  2. {  
  3.     // 7*6  
  4.     int result = 1;  
  5.     for (int i = number; i > 0; i--)  
  6.     {  
  7.         result = result * i;  
  8.     }  
  9.     return result;  
  10. }  
Calling above method:
  1. static void Main(string[] args)  
  2. {  
  3.     int factorial;  
  4.     factorial = clsfactorial.PrintFactorialOfNumber(5);  
  5.     Console.WriteLine("Factorial Of 5 Is " + factorial.ToString());  
  6.     factorial = clsfactorial.PrintFactorialOfNumber(7);  
  7.     Console.WriteLine("Factorial Of 7 Is " + factorial.ToString());  
  8.     Console.ReadLine();  
  9. }  
Output:
Get Factorial of number

 

Ebook Download
View all
Learn
View all