using System;
public class Program
{
public static void Main()
for(int n=1; n<5; n++)
Console.WriteLine("Answer: " + countandSay(n));
}
// Returns n'th term in look-and-say sequence
private static string countandSay(int n)
// Base cases
if (n == 1) return "1";
if (n == 2) return "11";
// Find n'th term by generating all terms from 3 to
// n-1. Every term is generated using previous term
string str = "11"; // Initialize previous term
for (int i = 3; i<=n; i++)
// In below for loop, previous character
// is processed in current iteration. That
// is why a dummy character is added to make
// sure that loop runs one extra iteration.
str += '$';
int len = str.Length;
int cnt = 1; // Initialize count of matching chars
string tmp = ""; // Initialize i'th term in series
// Process previous term to find the next term
for (int j = 1; j < len; j++)
// If current character does't match
if (str[j] != str[j-1])
// Append count of str[j-1] to temp
tmp += cnt + '0';
// Append str[j-1]
tmp += str[j-1];
// Reset count
cnt = 1;
// If matches, then increment count of matching
// characters
else cnt++;
// Update str
str = tmp;
return str;