Software & Finance





Visual C++ - Program to check whether a Number is a palindrome or not





 

Here is Visual C++ source code to check whether a given number is a palindrome or not.

 


Source Code


// Program for Palindrome of a number

//

 

#include <stdio.h> 

 

int main()

{

    long n, number;

    int digits[10], i, index;

    int palindrome;

 

    printf("\nProgram to check whether given number is palindrome or not. Enter -1 to exit");

    while(1)

    {

        printf("\nEnter a number:");

        scanf("%ld", &n);

       

        if(n < 0)

            break;

        number = n;

        index = 0;

        palindrome = 1;

        do

        {

            digits[index++] = n % 10;

            n = n / 10;

        } while(n > 0);

 

        for(i = 0; i < index / 2 + 1; i++)

        {

            if(digits[i] != digits[index - 1 - i])

            {

                palindrome = 0;

                break;

            }

        }

 

        if(palindrome == 1)

            printf("The number %d is a palindrome\n", number);

        else

            printf("The number %d is NOT a palindrome\n", number);

    }

      return 0;

}

 

Click here to download the source code and .EXE application

 

Output


Program to check whether given number is palindrome or not. Enter -1 to exit

 

Enter a number:121

The number 121 is a palindrome

 

Enter a number:565

The number 565 is a palindrome

 

Enter a number:98

The number 98 is NOT a palindrome

 

Enter a number:978

The number 978 is NOT a palindrome

 

Enter a number:-1