Q:

Find palindrome numbers from array using C# program

belongs to collection: C# Basic Programs | array programs

0

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.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | array programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Insert an element at given position into array usi... >>
<< Find smallest element from integer array in C#...