We are providing online training of realtime Live project on Asp.Net MVC with Angular and Web API. For more information click here. If you have any query then drop the messase in CONTACT FORM

Thursday, June 11, 2015

Check whether a given input number is palindrome or not ,if not then find the nearest palindrome and if input middle number of two palindrome numbers the display of both palindrome numbers


class Program 
    {

        public static Dictionary<char, int> Count(string stringToCount) 
        {

            Dictionary<char, int> charcount = new Dictionary<char, int>(); 

            foreach (var charchar in stringToCount) 
            {

                if (!charcount.ContainsKey(charchar)) 
                { 
                    charcount.Add(charchar, 1); 
                } 
                else 
                { 
                    charcount[charchar]++; 
                }

            }

            return charcount;

        }

        static void Main(string[] args)

        {

            Console.WriteLine("Enter the string :");

            string str = Console.ReadLine();

            var count = Program.Count(str);


            foreach (var character in count) 
            { 
                Console.WriteLine("{0}-{1}", character.Key, character.Value); 
            }

            Console.WriteLine("Total number of characters: {0}", str.Count());

            Console.ReadLine(); 
        }
  
    }   
class Program 
    {

        static void Main(string[] args) 
        { 
            int Num;

            Console.Write("Enter a Number : "); 
            Num = Convert.ToInt32(Console.ReadLine()); 
            if (checkPalindrome(Num)) 
            { 
                Console.WriteLine("it is a palindrome number"); 
            } 
            else 
            {

                Console.WriteLine("it is not a palindrome number"); 

                int frd = checkForward(Num);

                int rev = checkBack(Num); 

                checkSmall(frd, rev, Num); 
            }

            Console.ReadLine();

        }

        static Boolean checkPalindrome(int x) 
        { 
            int z;

            int number = x;

            int sum = 0;

            while (number > 0) 
            {
                 z = number % 10;


                sum = (sum * 10) + z;

                number = number / 10; 
            }

            if (sum == x) 
            { 
                return true; 
            } 
            else 
            { 
                return false; 
            }

        }


        static int checkForward(int x) 
        { 
            int next = x + 1;

            while (next > x) 
            {

                if (checkPalindrome(next)) 
                {

                    break;

                }

                next++;  
            }

            return next;

        }

        static int checkBack(int x) 
        {
             int Back = x - 1;

            while (Back < x) 
            {
                 if (checkPalindrome(Back)) 
                { 
                    break; 
                }

                Back--;
             }

            return Back;

        }

        static void checkSmall(int x, int y, int z) 
        {

            int a = x - z; 
            int b = z - y;

            if (a > b)

            {

                Console.WriteLine("The Nearest Palindrome number is " + y); 
            }

            else if (a < b) 
            {

                Console.WriteLine("The Nearest Palindrome number is " + x); 
            }

            else 
            {

                Console.WriteLine("The nearest two palindrome numbers are {0},{1}", x, y);

            }

        }  


    }

No comments: