Software Development Tricky

Friday, 28 April 2017

Palindrome Number in CSharp


Ø  A palindrome number has the reverse number is equal to original number

Sample Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
namespace Logical_Programs
{
    class palindrome_number
    {
        static void Main()
        {
            validatePalindromeNumber();
            Console.WriteLine("\nTo Continue Press 1");
            int checkOneMore = int.Parse(Console.ReadLine());
            if (checkOneMore == 1)
                validatePalindromeNumber();
            else
                Console.WriteLine("Press any key to continue...");
            Console.ReadLine();
        }

        public static void validatePalindromeNumber()
        {
            int num, temp, remainder, reverse = 0;
            Console.WriteLine("\nEnter an Integer \n");
            num = int.Parse(Console.ReadLine());
            temp = num;
            while (num > 0)
            {
                remainder = num % 10;
                reverse = reverse * 10 + remainder;
                num /= 10;
            }
            Console.WriteLine("\nGive number is = {0} \n", temp);
            Console.WriteLine("Its reverse is = {0} \n", reverse);
            if (temp == reverse)
                Console.WriteLine("Number is a palindrome \n");
            else
                Console.WriteLine("Number is not a palindrome \n");
        }
    }
}

Output:


If you like this post. Kindly share your feedback.

No comments:

Post a Comment

Followers