Search code examples
calgorithmpalindrome

Creating a function to check if a number is Palindrome


i am trying to make a function to return true if the number is palindrome and return false if the number is not , so first i created a function that reverse the number then another function to tell if the reversed number is equal to the original number , but it not returning for me the correct output , any help ?

 #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <math.h>
    //Reversing a number
    int DigitsReversing(int num)
    {
        int resultofR = 0;
        while (num > 0)
        {
            resultofR = resultofR * 10 + num % 10;
            num = num / 10;
        }
        return resultofR;
    }
    //telling if the original number equal to the reversed number if yes return if not return false
    int isPali(int num)
    {

        DigitsReversing(num);
        int resultofR;
        if (num == resultofR)
            return true;
        else
            return false;
        return 0;


    }

    //calling the function 
    int main()
    {
        int num;
        scanf("%d", &num);
        printf("%d", isPali(num));
        return 0;
    }

edited **** :

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
int DigitsReversing(int num)
{
    int resultofR = 0;
    while (num > 0)
    {
        resultofR = resultofR * 10 + num % 10;
        num = num / 10;
    }
    return resultofR;
}


bool isPali(int num , int resultofR)
{
    resultofR = DigitsReversing(num);
    if (num == resultofR)
        return true;
    else
        return false;


}


int main()
{
    int num, resultofR;
    scanf("%d", &num);
    resultofR = DigitsReversing(num);
    isPali(num, resultofR);

Solution

  • C does have a boolean datatype. Usually, 0 is false and 1 is true. They return value 1 for true and 0 for false. As @exnihilo and @bob__ suggested C does have boolean datatypes.

    #include<stdio.h>
    #include<math.h>
    //Reversing a number
    int DigitsReversing(int num)
    {
        int resultofR = 0;
        while (num > 0)
        {
            resultofR = resultofR * 10 + num % 10;
            num = num / 10;
        }
        return resultofR;
    }
    //telling if the original number equal to the reversed number if yes return if not return false
    void isPali(int num)
    {
        int resultofR= DigitsReversing(num);
        if (num == resultofR)
            printf("True");
        else
        printf("False");
    
    
    }
    
    //calling the function 
    int main()
    {
        int num;
        scanf("%d",&num);
        isPali(num);
        return 0;
    }
    

    OUTPUT For non-palindrome numbers.

    9998
    False
    --------------------------------
    Process exited after 10.35 seconds with return value 0
    Press any key to continue . . .
    

    For palindrome numbers.

    9999
    True
    --------------------------------
    Process exited after 10.35 seconds with return value 0
    Press any key to continue . . .