RSS

Palindrome Numbers Between 1 and 10000

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

namespace Q2
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int k = 1; k <= 10000; k++)
            {

                string data = k.ToString();               //converts the int value of k into sting

                bool isPalindrome = true;

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

                    if (data[i] != data[(data.Length - 1) - i])
                    {
                        isPalindrome = false;
                        break;
                    }
                }

                if (isPalindrome)
                {
                    Console.WriteLine("Palindrome :" + k);
                }
            }


            Console.ReadKey();
        }
    }
}

Output

Advertisements
 
2 Comments

Posted by on May 23, 2011 in C#

 

Tags: , , , , , , , ,

GCD

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

namespace GCD
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.Write("Enter Value of X : ");
            int x = int.Parse(Console.ReadLine());

            Console.Write("Enter Value of Y : ");
            int y = int.Parse(Console.ReadLine());

            int ans;

            if (x > y)
            {
               ans = gcd(x, y);
            }

            else
            {
              ans = gcd(y, x);
            }

            Console.Write("The GCD is : " + ans);

            Console.ReadKey();

        }

        static int gcd(int x, int y)
        {
            int z=0;
            while (x != y)
            {
                z = x - y;
                if (z > y)
                {
                    x = z;
                }

                else
                {
                    x = y;
                    y = z;
                }
            }

            return z;
        }
    }
}
 
Leave a comment

Posted by on May 11, 2011 in C#

 

Tags:

Counting Whole Numbers in a String

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

namespace int_count
{
    class Program
    {
        static void Main(string[] args)
        {
            string data ="409867545387454082354";
                IList<int> digits = new List<int> ();
                for (int j = 0; j < 10; j++)
                {
                    digits.Add(0);
                }

                for (int i = 0; i < data.Length; i++)
                {
                    digits[int.Parse(data.Substring(i, 1))]++;    //Sub String          
                }
                for (int k = 0; k < 10; k++)
                {
                    Console.WriteLine(k + " = " + digits[k]);
                }
                Console.ReadKey();
        }
    }
}
 
Leave a comment

Posted by on May 11, 2011 in C#

 

Tags:

Prime Numbers between ‘0’ and ‘100’

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

namespace PrimeNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            bool isPrime = true;

            for (int i = 1; i <= 100; i++)
            {
                for (int j = 2; j <= 100; j++)
                {

                    if (i != j && i % j == 0)
                    {
                        isPrime = false;
                        break;
                    }

                }

                if (isPrime)
                {
                    Console.WriteLine("Prime:" + i);
                }

                isPrime = true;
            }

            Console.ReadKey();
        }
    }
}

 
5 Comments

Posted by on May 11, 2011 in C#

 

Tags: ,

Addition of Matrices

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.Write("Please enter No. of Rows :: ");
            int row = int.Parse(Console.ReadLine());

            Console.Write("Please enter No. of Columns :: ");
            int column = int.Parse(Console.ReadLine());
            
            int[,] matrix1 = new int[row, column];
            int[,] matrix2 = new int[row, column];
            int[,] matrix3 = new int[row, column];

            Console.WriteLine("Please provide matrix data :: for " + row + " X " + column);

            for(int k=0; k< row; k++)
            {
                 for(int l=0; l<column; l++)
                 {
                     Console.Write("Please Input for " + k + "th row and " + l + "th column of first Matrix :: ");
                      matrix1[k,l] = int.Parse(Console.ReadLine());
                 }
            }

            for (int k = 0; k < row; k++)
            {
                for (int l = 0; l < column; l++)
                {
                    Console.Write("Please Input for " + k + "th row and " + l + "th column of second Matrix :: ");
                    matrix2[k, l] = int.Parse(Console.ReadLine());
                }
            }

            for (int k = 0; k < row; k++)
            {
                for (int l = 0; l < column; l++)
                {
                    matrix3[k, l] = matrix1[k, l] + matrix2[k, l];
                }
            }


            Console.WriteLine("Sum of Matrix is following: ");

            for (int k = 0; k < row; k++)
            {
                for (int l = 0; l < column; l++)
                {
                    Console.Write(matrix3[k, l] + " ");
                }

                Console.WriteLine();
            }

            Console.ReadKey();

        }
    }
}
 
3 Comments

Posted by on May 11, 2011 in C#

 

Tags:

Reverse a String

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

namespace Quiz1
{
    class Program
    {
        static void Main(string[] args)
        {
            string data = "ABCD";

            for (int i = data.Length - 1; i >= 0; i--)
            {
                Console.WriteLine(data[i]);
            }

            Console.ReadKey();

        }
    }
}
 
Leave a comment

Posted by on May 11, 2011 in C#

 

Tags:

Palindrome

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

namespace Q2
{
    class Program
    {
        static void Main(string[] args)
        {
            string data = "ABBDDAADDBBA";
            
           bool isPalindrome  = true;

            for (int i = 0; i < data.Length / 2; i++)
            {
                
                if (data[i] != data[(data.Length-1)-i])
                {
                    isPalindrome  = false;
                    break;
                }
           }

            if (isPalindrome)

            {
                Console.WriteLine("This word is a palindrome");
            }
            else
            {
                Console.WriteLine("This word is not a palindrome");
            }


            Console.ReadKey();
        }
    }
}

 
2 Comments

Posted by on May 7, 2011 in C#

 

Tags: