Home » .Net

Find palindrome numbers from array using C# program

In this C# program, we will learn how to find palindrome numbers from list of integers? Here we will create an array that contains list of integers.

Given an array of integers and we have to find palindrome numbers from given elements.

To find palindrome numbers from array: We check each number; if number is equal to its reveres then it will be a palindrome number.

To find palindrome number, we will traverse array and check each elements with its reverse number (which will be calculating in the program), if element will equal to its reverse, number will be palindrome and we will print the palindrome numbers.

For example we have list of integers: 182, 12321, 84, 424, 271

Here,
182 is not a palindrome number because it is not equal to its reverse.
12321 is a palindrome number because it is equal to its reverse.
84 is not a palindrome number because is not equal to its reverse.
424 is a palindrome number because it is equal to its reverse.
271 is not a palindrome number because it is not equal to its reverse.

Consider the 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 





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.