C Program to Check Whether a Number is Palindrome or Not

C Program to Check Whether a Number is Palindrome or Not

C program to check whether a number is palindrome or not; Through this tutorial, we will learn how to check whether a number is palindrome or not in the c program using for loop, while loop, recursion and function.

Algorithm To Check A Number Is Palindrome Or Not

Use the following steps to write a program to check whether a number is palindrome or not; as follows:

  • Take the number as input from the user
  • Reverse a given number.
  • Compare the original number with the reverse value.
  • If they matched, then it is a palindrome number. Otherwise, it is not a palindrome number in C programming.

Programs To Check A Number Is Palindrome Or Not in C

  • C Program To Check A Number Is Palindrome Or Not using For Loop
  • C Program To Check A Number Is Palindrome Or Not using While Loop
  • C Program To Check A Number Is Palindrome Or Not using Recursion
  • C Program To Check A Number Is Palindrome Or Not using Function

C Program To Check A Number Is Palindrome Or Not using For Loop

#include<stdio.h>

void main()
{
    int i,n,r,s=0;
     
    printf("\n Enter Integer Number:");
    scanf("%d",&n);
     
    //LOOP TO FIND REVERSE OF A NUMBER
    for(i=n;i>0; )
    {
        r=i%10;
        s=s*10+r;
        i=i/10;
    }
     
    /* CHECKING IF THE NUMBER ENTERED AND THE REVERSE NUMBER IS EQUAL OR NOT */
    if(s==n)
    {
        printf("\n %d is a Palindrome Number",n);
    }
    else
    {
        printf("\n %d is not a Palindrome Number",n);
    }
    return 0;
}

The output of the above c program; as follows:

Enter Integer Number:312
312 is not a Palindrome Number

C Program To Check A Number Is Palindrome Or Not using While Loop

/* C program to check whether a number is palindrome or not */

#include <stdio.h>

int main()
{
    int number, revNumber = 0, rem = 0, tempNumber;

    printf("Enter an integer number:- ");
    scanf("%d", &number);

    tempNumber = number;

    while (tempNumber != 0) {
        rem = tempNumber % 10;
        revNumber = revNumber * 10 + rem;
        tempNumber /= 10;
    }

    /* checking if number is equal to reverse number */
    if (revNumber == number)
        printf("%d is a palindrome.", number);
    else
        printf("%d is not a palindrome.", number);

    return 0;
}

The output of the above c program; as follows:

Enter an integer number:- 11
11 is a palindrome.

C Program To Check A Number Is Palindrome Or Not using Recursion

/**
 * C program to check palindrome number using recursion
 */
 
#include <stdio.h>
#include <math.h>


/* Function declarations */ 
int reverse(int num);
int isPalindrome(int num);



int main()
{
    int num;
    
    /* Input any number from user */
    printf("Enter any number: ");
    scanf("%d", &num);
    
    if(isPalindrome(num) == 1)
    {
        printf("%d is palindrome number.\n", num);
    }
    else
    {
        printf("%d is NOT palindrome number.\n", num);
    }
    
    return 0;
}



/**
 * Function to check whether a number is palindrome or not.
 * This function returns 1 if the number is palindrome otherwise 0.
 */
int isPalindrome(int num)
{
    /* 
     * Check if the given number is equal to 
     * its reverse.
     */
    if(num == reverse(num))
    {
        return 1;
    }
    
    return 0;
}


/**
 * Recursive function to find reverse of any number
 */
int reverse(int num)
{
    /* Find number of digits in num */
    int digit = (int)log10(num);
    
    /* Recursion base condition */
    if(num == 0)
        return 0;

    return ((num%10 * pow(10, digit)) + reverse(num/10));
}

The output of the above c program; as follows:

Enter an integer number:- 121
121 is a palindrome.

C Program To Check A Number Is Palindrome Or Not using Function

/* C program to check whether a number is palindrome or not */

#include <stdio.h>

/*function to check Palindrome Number*/
int isPalindrome(int num)
{
    int tempNumber = num;
    int dig, revNumber;

    /*getting reverse number*/
    revNumber = 0;
    while (num > 0) {
        dig = num % 10;
        revNumber = (revNumber * 10) + dig;
        num /= 10;
    }

    if (revNumber == tempNumber)
        return 1; /*Palindrome Number*/
    else
        return 0; /*Not a Palindrome Number*/
}

int main()
{
    int number;

    printf("Enter an integer number:- ");
    scanf("%d", &number);

    if (isPalindrome(number))
        printf("%d is a palindrome.", number);
    else
        printf("%d is not a palindrome.", number);

    return 0;
}

The output of the above c program; as follows:

Enter an integer number:- 121
121 is a palindrome.

Recommended C Programs

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *