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

LogicalQuestionC#-converted

The document presents a collection of logical programming questions and their solutions in C#. It includes examples such as swapping two integers without a third variable, checking for prime numbers, generating Fibonacci series, and calculating factorials. Each question is accompanied by C# code snippets demonstrating the solutions.

Uploaded by

Harindra Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

LogicalQuestionC#-converted

The document presents a collection of logical programming questions and their solutions in C#. It includes examples such as swapping two integers without a third variable, checking for prime numbers, generating Fibonacci series, and calculating factorials. Each question is accompanied by C# code snippets demonstrating the solutions.

Uploaded by

Harindra Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

C# Logical Questions with Answer

By Tapan Dubey on Dec 17, 2015

I am sharing few logical questions with their solution in C#.

• 3.2k

• 1

• 0

• facebook

• twitter

• linkedIn

• google Plus

• Reddit










• expand

Download 100% FREE Spire Office APIs

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

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


would be:
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

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 Nu
mber" : "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:
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:
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:

Logical Interview Questions Using C#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Program obj = new Program();
obj.swap(); Console.WriteLine();

obj.Factorial(); Console.WriteLine();

obj.Fabbonic(10); Console.WriteLine();

obj.TriUpToDown(); Console.WriteLine();

obj.TriDownToUp(); Console.WriteLine();

obj.InterchageNebhorVaules(); Console.WriteLine();

obj.InterchageFirstToLastSoOn(); Console.WriteLine();

obj.xMasTree(); Console.WriteLine();

obj.LeapYear(2016); Console.WriteLine();
Console.ReadLine();
}

protected void swap()


{
int a = 5, b = 6;
Console.WriteLine(a.ToString() + " " + b.ToString());
b = a + b;
a = b - a;
b = b - a;
Console.WriteLine(a.ToString() + " " + b.ToString());
}

protected void Factorial()


{
int a = 7;
int c = 1;
for (int i = 1; i <= a; i++)
{
c = c * i;
}
Console.WriteLine(c.ToString());
}

protected void Fabbonic(int till)


{
int a = 1, b = 1;
int c;
Console.Write(a.ToString() + " " + b.ToString() + " ");
for (int i = 1; i < till; i++)
{
c = a + b;
a = b;
b = c;
Console.Write(c.ToString() + " ");
}
}

protected void TriUpToDown()


{
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < i; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
}

protected void TriDownToUp()


{
for (int i = 0; i < 6; i++)
{
for (int j = i; j < 6; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
}
protected void xMasTree()
{
int a = 0;
for (int i = 0; i < 8; i++)
{
for (int k = i; k < 8; k++)
{
Console.Write(" ");
}
for (int j = 0; j < i; j++)
{
Console.Write("* ");
}
Console.WriteLine();
a++;
if (a == 3 || a == 7)
i = 1;
}
}

protected void InterchageNebhorVaules()


{
string input = "NITISH";
Char[] arrInput = input.ToCharArray();

StringBuilder output = new StringBuilder();


for (int i = 0; i < arrInput.Length / 2; i++)
{
output.Append(arrInput[i + 1].ToString());
output.Append(arrInput[i].ToString());
}

Console.WriteLine(input + " >> " + output.ToString());


}

protected void InterchageFirstToLastSoOn()


{
string input1 = "NITISH";
StringBuilder output1 = new StringBuilder();
StringBuilder output2 = new StringBuilder();

char[] characters1 = input1.ToCharArray();

for (int i = 0; i < characters1.Length / 2; i++)


{
output1.Append(characters1[characters1.Length - (i + 1)]);
output2.Append(characters1[i]);
}
Console.Write(input1.ToString() + " >--> " + output1.ToString() +
Operation.ReverseString(output2.ToString()));
}

protected void LeapYear(int yr)


{
if ((yr % 4 == 0 && yr % 100 != 0) || (yr % 400 == 0))
{
Console.WriteLine(yr.ToString() + " is a leap year.");
}
else
{
Console.WriteLine(yr.ToString() + " is not a leap year.");
}
Console.Read();
}
}

static class Operation


{
/// <summary>
/// Receives string and returns the string with its letters reversed.
/// </summary>
public static string ReverseString(string s)
{
char[] arr = s.ToCharArray();
Array.Reverse(arr);
return new string(arr);
}
}
}

Posted by Nitish Kumar at 23:05


Email ThisBlogThis!Share to TwitterShare to FacebookShare to Pinterest

2 comments:
.

Anandan M28 September 2013 at 05:12

For 3rd question, below is the better answer...

private static void xmastree1()


{
int k = 10;
for (int j = 0; j < k; j++)
{

for (int i = 0; i <= (j + k); i++)


{
if (i >= (k - j) && i <= (k + j))
{
Console.Write("*");
Console.Write("\t");
}
else
{
Console.Write("\t");
}
}
Console.Write("\n");
}

}
.

Reply

Nitish Kumar28 September 2013 at 05:31

Dear Anandam,

I think you should run this program again.

Reply

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy