Find palindrome numbers from array using C# program

Learn, how to find palindrome numbers from list of integers?
[Last updated : March 19, 2023]

Finding palindrome numbers from an array

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.

Example

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.

C# program to find palindrome numbers from an array

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 

C# Basic Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.