1

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);
VillageTech
  • 1,968
  • 8
  • 18
MajdKh
  • 15
  • 3

2 Answers2

1

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 . . .
Ch3steR
  • 20,090
  • 4
  • 28
  • 58
  • can u check my edited code ? i need to specifically return true or false and not 1 or zero ... i edited the code and included stdbool library – MajdKh Dec 07 '19 at 13:25
  • Your edited code is wrong and it would return true for any number passed to it. because you are using `DigitsReversing` two times on a number. You can use my edit my code by replacing 1 with true and 0 with false and include `` . – Ch3steR Dec 07 '19 at 13:44
  • i tried to replace and include it still return 1 and 0 – MajdKh Dec 07 '19 at 13:45
  • In C usually we use 0 , 1 for true ,false respectively. And compiler treats any non-zero number as true and 0 to be false. Hope this helped you. – Ch3steR Dec 07 '19 at 13:46
  • i know , and thanks u helped , but due what i have in my assignment i must return true or false and not 1 or zero , so any suggestion how i can make this ? i replaced how you said and still it return for me 1 or zero and true or false , thanks for the help ! – MajdKh Dec 07 '19 at 13:49
  • Its would return 1 , 0 because you are printing it using `%d`. If you want True and False to be displayed then you can use a simple printf statement. let me edit my answer to show you. – Ch3steR Dec 07 '19 at 13:52
  • 1
    "C doesn't have a boolean datatype" -- [what about `bool` and `_Bool`?](https://stackoverflow.com/a/1608350/6879826) – ad absurdum Dec 19 '19 at 15:27
  • bool will hold either 0 or 1. In the question he wanted `True` or `False` to be returned. – Ch3steR Dec 19 '19 at 15:29
  • A function that returns a `bool` _does_ return `true` or `false`. Boolean types are sort of second class citizens in C, but C does have a boolean type. – ad absurdum Dec 19 '19 at 15:43
  • @exnihilo I have added an answer for you in this thread please check it out. – Ch3steR Dec 19 '19 at 15:44
  • @exnihilo `if(true)` is equivalent to `if(1)` . Like python has `True` and `False` as keywords. In C you don,t have them. They are preprocessors `#define true 1` and #define false 0` . – Ch3steR Dec 19 '19 at 15:50
  • 1
    `bool` is a typedef for `_Bool`, and under the hood `_Bool` is an integer type (and as such is subject to integer promotions, which can cause some confusion), but `_Bool` is still a native boolean type in C, i.e., `_Bool` is not a typedef of anything. – ad absurdum Dec 19 '19 at 15:57
  • @exnihilo In the question he wanted the function to return `true`. In C if return `true` and print it you would print `1`. All I was making him understand that. – Ch3steR Dec 19 '19 at 16:02
  • @exnihilo I have edited the answer and thanks for taking your time for me to make me understand. Appreciate it. – Ch3steR Dec 19 '19 at 16:17
0

The posted snippets show some confusion about fundamental topics like the correct way to pass arguments to a function and collect the returned value or just the differnece between an "output" and a "returned" value.

Consider this, for example:

bool isPali(int num , int resultofR)
{ //                      ^^^^^^^^^           It's passed to the function...
    resultofR = DigitsReversing(num);  // <-  and immediately overwritten by another value   
    if (num == resultofR)
        return true;
    else
        return false;
}

int main()
{
    int resultofR;
    // ...
    resultofR = DigitsReversing(num);  // <-- This is effectually called twice
    isPali(num, resultofR);            // <-- The returned value is ignored       
}

You just need to write one function

#include <stdbool.h>

bool is_palindrome(long num)
{
    long reversed = 0;

    for (long i = num; i > 0; i /= 10)
    {
        reversed = reversed * 10 + i % 10;
    }
    return reversed == num;
}

Then, call it and interpret its return value properly, to generate the wanted output

int number;
scanf("%d", &number);
printf("%s\n", is_palindrome(number) ? "True" : "False");
Bob__
  • 12,361
  • 3
  • 28
  • 42
  • Thanks for taking your time and making us understand. Really appreciate it. Understood what you were trying to explain. – Ch3steR Dec 19 '19 at 16:32