1. How to reverse a string?
string
aa = "Hello World!";
string
str = new string(aa.Reverse().ToArray());
OR
string
aa = "Hello World!";
var
result = aa.ToCharArray().Reverse();
Console.WriteLine(new
string(result.ToArray()));
OR
internal static void
ReverseString(string str)
{
char[] charArray = str.ToCharArray();
for (int i = 0, j = str.Length - 1; i < j; i++, j--)
{
charArray[i] = str[j];
charArray[j] = str[i];
}
string reversedstring = new string(charArray);
Console.WriteLine(reversedstring);
}
2. Prime number?
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;
}
3.
Fibonacci series?
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);
}
}
4.
How to find if the given string is a palindrome or not?
public bool isPalindrome(string
myString)
{
string string1, rev;
string1 = "Malayalam";
char[] ch = string1.ToCharArray();
Array.Reverse(ch);
rev = new string(ch);
bool b = string1.Equals(rev, StringComparison.OrdinalIgnoreCase);
if (b == true)
{
Console.WriteLine("" + string1 + " is a
Palindrome!");
}
else
{
Console.WriteLine(" " + string1 + " is not a
Palindrome!");
}
return b;
}
5.
Get Factorial of number.
public static int
PrintFactorialOfNumber(int number)
{
// 5*4
int result = 1;
for (int i = number; i > 0; i--)
{
result = result * i;
}
return result;
}
6.
How to remove duplicate characters from a string?
internal static void
removeduplicate(string str)
{
string result = string.Empty;
for (int i = 0; i < str.Length; i++)
{
if (!result.Contains(str[i]))
{
result += str[i];
}
}
Console.WriteLine(result);
}
7. Find third Max salary using LINQ?
List<Employee> employees = new List<Employee>()
{
new Employee { Id = 1, UserName = "Anil" , Salary = 5000},
new Employee { Id = 2, UserName = "Sunil" , Salary = 6000},
new Employee { Id = 3, UserName = "Lokesh" , Salary = 8000},
new Employee { Id = 4, UserName = "Vinay" , Salary = 7000},
new Employee { Id = 5, UserName = "Amit" , Salary = 8000},
new Employee { Id = 6, UserName = "Lokesh" , Salary = 5500},
new Employee { Id = 7, UserName = "Mahesh" , Salary = 7000}
};
var emp = employees.GroupBy(e => e.Salary).OrderByDescending(g => g.Key).Skip(2).First();
8. Join in LINQ?
var query = from department in departments
join employee in employees
on department.ID equals employee.DepartmentID
select new { employee.Name, Department = department.Name };
9. Left Outer Join in LINQ?
var Ljoin = from emp in ListOfEmployees
join proj in ListOfProject
on emp.ProjectID equals proj.ProjectID into JoinedEmpDept
from proj in JoinedEmpDept.DefaultIfEmpty()
select new
{
EmployeeName = emp.Name,
ProjectName = proj != null ? proj.ProjectName : null
};
No comments:
Post a Comment