Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Basically Im reversing number and checking if they match or not. Even though number matches I am not getting the output

+5 votes
asked Oct 18, 2021 by Farhaad DotaAllstars (170 points)
#include <stdio.h>
#include <math.h>
int main()
{
    int i,a,b,c=0,n=4;
    scanf("%d",&i);
    while(i>0)
    {
        a=i%10;
        b=a*(pow(10,n));
        c=c+b;
        i=i/10;
        n=n-1;
    }
   if(c==i)
   {
       printf("%d\n",c);
       printf("Numbers are identical");
   }
       else
       {
           printf("%d\n",c);
        printf("Numbers are not identical");
       }
    return 0;
}

1 Answer

0 votes
answered Oct 18, 2021 by Peter Minarik (55,660 points)
edited Oct 18, 2021 by Peter Minarik

You read the original number into the variable i, which you keep modifying later in the code until it actually reaches the value 0. So later on when you compare the i to c, you actually compare 0 to c and not the original input from the user.

Furthermore, your code only works for exactly 5 digit long non-negative integral numbers and 0.

Below is a solution for any int input:

#include <stdio.h>

int main()
{
    // Read a number from the user
    int number;
    printf("Please, enter a number: ");
    scanf("%d", &number);

    // Reverse the order of digits
    int reversedNumber = 0;
    for (int tmp = number; tmp > 0; tmp /= 10)
    {
        reversedNumber *= 10;
        reversedNumber += tmp % 10;
    }

    // Print the result
    if (number == reversedNumber)
        printf("The number reads the same backward.\n");
    else
        printf("The number does not read the same backward.\n");

    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...