0

This is a program to find whether a given number is a palindrome or not. In this case the condition in the if statement even though it is true isnt getting executed and the program is terminationg.

#include <stdio.h>
#include <math.h>
int count(int n)
{
    int counts;
    counts = 0;
    do{
        n = n/10;
        counts++;

    }while(n!=0);
    return counts;
}
int main()
{
    int i=0;
    int numb;
    printf("Enter a number: \n");
    scanf("%d",&numb);
    int num1=numb;
    int num2=numb;
    int c;
    int power;
    int n1,n2;
    int c_num = count(numb);
    do{
        c = count(num1);
        if((num1/pow(10,c-1))==(num2%10)){
            power = pow(10,c-1);
            n1 = num1 % power;
            n2 = num2/10;
            num1 = n1;
            num2 = n2;
            i++;}
        else{
            num1=0;
            num2=0;
        }
    }while(num1!=0);
    if (c_num==i){
        printf("It is a Palindrome number.");
    }
    else{
        printf("It is not a Palindrome number.");
    }
    return 0;
}
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
pranftw
  • 118
  • 1
  • 9
  • Can you explain which `if` statement you are referring to, what you believe it is testing, and why you think it is not working properly? – Scott Hunter Mar 06 '20 at 20:25
  • 3
    I assume it's the `if((num1/pow(...`, which is testing floating point numbers for equality--a really bad idea. Write your own integer `pow10()` function. – Lee Daniel Crocker Mar 06 '20 at 20:28
  • 2
    Enter the number as a string, and treat it the same way as a word palindrome. Simple. – Weather Vane Mar 06 '20 at 20:50
  • the if statement if((num1/pow(10,c-1))==(num2%10)){ – pranftw Apr 02 '20 at 14:33
  • actually Eugenio Hernan already has already provided the answer. I was actually trying to compare a double(from the pow operation) and an int because of which I was getting errors. but that was solved with Eugenio's suggestion that it should be casted to int!! – pranftw Apr 02 '20 at 14:35

1 Answers1

-1

Answering exactly what you asked for: pow() returns a double and you are comparing that to an int, that is the problem.

Simple Solution: You missed the cast to int, do this:

if ((num1 / (int)pow(10, c - 1)) == (num2 % 10))
  • 1
    Did you mean `(int)round(num1 / pow(10, c - 1))`? One problem is the inexactness of floating point arithmetic. `pow(10, 2)` can be `99.999999999999`. – Weather Vane Mar 06 '20 at 21:59
  • And `99.9999999999` cast to `int` results in `99`. See https://stackoverflow.com/questions/21452711/pow-seems-to-be-out-by-one-here – Andrew Henle Mar 06 '20 at 22:04
  • No, for this algorithm there is no need to round, actually it should be cast in the result of pow, I will edit that. – Eugenio Hernan Mar 06 '20 at 22:12
  • Please see [Why do the square of input decreases by one when using pow function?](https://stackoverflow.com/questions/60572057/why-do-the-square-of-input-decreases-by-one-when-using-pow-function) – Weather Vane Mar 06 '20 at 22:27
  • Again that is not relevant for this algorithm, and he is not asking that at all!, please try to understand the algorithm first. – Eugenio Hernan Mar 06 '20 at 22:33
  • Thanks a lot for your reply!!! I changed the code and it seems to work flawlessly now! – pranftw Apr 02 '20 at 14:30