Programs
string str = "ssmmmjjkkkkrrr";var counts = str.GroupBy(c => c).Select(g => new { Char = g.Key, Count = g.Count() });foreach(var c in counts){ Console.WriteLine("{0} = {1}", c.Char, c.Count);}
class GroupExample1 { static void Main() { // Create a data source.string[] words = { "blueberry", "chimpanzee", "abacus", "banana", "apple", "cheese" }; // Create the query.var wordGroups = from w in words group w by w[0]; // Execute the query.foreach (var wordGroup in wordGroups) { Console.WriteLine("Words that start with the letter '{0}':", wordGroup.Key); foreach (var word in wordGroup) { Console.WriteLine(word); } } // Keep the console window open in debug mode Console.WriteLine("Press any key to exit."); Console.ReadKey(); } } /* Output: Words that start with the letter 'b': blueberry banana Words that start with the letter 'c': chimpanzee cheese Words that start with the letter 'a': abacus apple */
class GroupClauseExample2 { static void Main() { // Create the data source.string[] words2 = { "blueberry", "chimpanzee", "abacus", "banana", "apple", "cheese", "elephant", "umbrella", "anteater" }; // Create the query.var wordGroups2 = from w in words2 group w by w[0] into grps where (grps.Key == 'a' || grps.Key == 'e' || grps.Key == 'i' || grps.Key == 'o' || grps.Key == 'u') select grps; // Execute the query.foreach (var wordGroup in wordGroups2) { Console.WriteLine("Groups that start with a vowel: {0}", wordGroup.Key); foreach (var word in wordGroup) { Console.WriteLine(" {0}", word); } } // Keep the console window open in debug mode Console.WriteLine("Press any key to exit."); Console.ReadKey(); } } /* Output: Groups that start with a vowel: a abacus apple anteater Groups that start with a vowel: e elephant Groups that start with a vowel: u umbrella */
using System;
namespace palindrome
{
class Program
{
static void Main(string[] args)
{
string s,revs="";
Console.WriteLine(" Enter string");
s = Console.ReadLine();
for (int i = s.Length-1; i >=0; i--) //String Reverse
{
revs += s[i].ToString();
}
if (revs == s) // Checking whether string is palindrome or not
{
Console.WriteLine("String is Palindrome \n Entered String Was {0} and reverse string is {1}", s, revs);
}
else
{
Console.WriteLine("String is not Palindrome \n Entered String Was {0} and reverse string is {1}", s, revs);
}
Console.ReadKey();
}
}
}
class Fibonacci
{
static void Main()
{
int i, count, f1 = 0, f2 = 1, f3 = 0;
Console.Write("Enter the Limit : ");
count = int.Parse(Console.ReadLine());
Console.Write(f1 + " ");
Console.Write(f2 + " ");
for (i = 2; i < count; i++)
{
f3 = f1 + f2;
Console.Write(f3 + " ");
f1 = f2;
f2 = f3;
}
Console.WriteLine("");
Console.WriteLine("Using Recursive");
Fibonacci_Recursive(count);
Console.WriteLine("Find Nth value Recursive");
int Fib = GetNthFibonacci_Ite(count);
Console.WriteLine(Fib);
Console.ReadLine();
Console.ReadKey();
}
public static int GetNthFibonacci_Ite(int n)
{
int number = n - 1; //Need to decrement by 1 since we are starting from 0
int[] Fib = new int[number + 1];
Fib[0] = 0;
Fib[1] = 1;
for (int i = 2; i <= number; i++)
{
Fib[i] = Fib[i - 2] + Fib[i - 1];
}
return Fib[number];
}
public static void Fibonacci_Recursive(int len)
{
Fibonacci_Rec_Temp(0, 1, 1, len);
}
private static void Fibonacci_Rec_Temp(int a, int b, int counter, int len)
{
if (counter <= len)
{
Console.Write("{0} ", a);
Fibonacci_Rec_Temp(b, a + b, counter + 1, len);
}
}
}
C# Program to find Product of 2 Numbers using Recursion
using System;
class program
{
public static void Main()
{
int a, b, result;
Console.WriteLine("Enter two numbers to find their product: ");
a = int.Parse(Console.ReadLine());
b = int.Parse(Console.ReadLine());
prog pg = new prog();
result = pg.product(a, b);
Console.WriteLine("Product of {0} and {1} is {2}",a, b, result);
Console.ReadLine();
}
}
class prog
{
public int product(int a, int b)
{
if (a < b)
{
return product(b, a);
}
else if (b != 0)
{
return (a + product(a, b - 1));
}
else
{
return 0;
}
}
}
C# Program to Reverse a Number & Check if it is a Palindrome
using System;
class program
{
public static void Main()
{
int num, temp, remainder, reverse = 0;
Console.WriteLine("Enter an integer \n");
num = int.Parse(Console.ReadLine());
temp = num;
while (num > 0)
{
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
Console.WriteLine("Given number is = {0}", temp);
Console.WriteLine("Its reverse is = {0}", reverse);
if (temp == reverse)
Console.WriteLine("Number is a palindrome \n");
else
Console.WriteLine("Number is not a palindrome \n");
Console.ReadLine();
}
}