Find Palindrome Numbers From Array Using C# Program

You are currently viewing Find Palindrome Numbers From Array Using C# Program

Find palindrome numbers from array using C# program

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

namespace ConsoleApplication1
{
    class Program
    {
        static int isPalindrome(int item)
        {
            int rev = 0;
            int rem = 0;
            int num = item;

            while (num > 0)
            {
                rem = num % 10;
                rev = rev * 10 + rem;
                num = num / 10;
            }

            if (rev == item)
                return 1;
            else
                return 0;
        }

        static void Main()
        {
            int i    = 0 ;
           
            int[] arr = new int[5];

            //Read numbers into array
            Console.WriteLine("Enter elements : ");
            for (i = 0; i < arr.Length; i++)
            {
                Console.Write("Element[" + (i + 1) + "]: ");
                arr[i] = int.Parse(Console.ReadLine());
            }

            //Loop to travers a array                
            Console.WriteLine("Palindrom items are : ");
            for (i = 0; i < arr.Length; i++)
            {
                if(isPalindrome(arr[i])==1)
                    Console.Write(arr[i]+" ");
            }
        }
    }
}

Output

Enter elements :
Element[1]: 182
Element[2]: 12321
Element[3]: 84
Element[4]: 424
Element[5]: 271
Palindrom items are :
12321 424 

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply