0

The goal of my program is to reverse the digits of a five-digit number and find if the reversed number is same or different from the original number.

However, for my given code if I give the input 111111 it gives a different output.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
  int num;
  long int a,reversenum=0;
 
  printf("Enter the 5 digit number(Not Greater Than 32767):\n");
  scanf("%d",&num);
 
  a=num%10;
  num= num/10;
  reversenum=reversenum+a*10000;
 
  a=num%10;
  num=num/10;
  reversenum=reversenum+a*1000;
 
  a=num%10;
  num=num/10;
  reversenum=reversenum+a*100;
 
  a=num%10;
  num=num/10;
  reversenum=reversenum+a*10;
 
  a=num%10;
  num=num/10;
  reversenum=reversenum+a*1;
 
  printf("The Number After Reverse is:%d\n",reversenum);
 
  if( num==reversenum )
    printf("same\n");
  else
    printf("different\n");
  
  return 0;
}

The original number and reversed number are the same but this program says they are different. What is wrong?
whiterook6
  • 3,270
  • 3
  • 34
  • 77
Rahul Rai
  • 51
  • 1
  • 1
  • 4
  • 3
    You're breaking you own requirement: "Enter the 5 digit number(Not Greater Than 32767)". – Fiddling Bits Oct 26 '20 at 18:00
  • [this](https://stackoverflow.com/questions/28572952/reverse-digits-of-an-integer) might be helpful and please do check on the web do your research and then ask a question with proper formatting. – 0_0perplexed Oct 26 '20 at 18:01
  • Does this answer your question? [Reverse digits of an integer](https://stackoverflow.com/questions/28572952/reverse-digits-of-an-integer) – 0_0perplexed Oct 26 '20 at 18:02
  • The `[c]` tag is sufficient. You don't need to state "C language" two more times in the title. – tadman Oct 26 '20 at 18:03
  • The number `100` (or `00100`) is equal to its reverse? – pmg Mar 01 '22 at 15:22

3 Answers3

1

if i give the output 111111 it give the output different

Because it is 6 digits number, not 5. Your code does not support longer integers than 5 decimal digits.

You should try to use loops for this task not to hardcode every possible digit (I believe that is the purpose of the exercise)

unsigned reverse(unsigned val)
{
    unsigned result = 0;
    while(val)
    {
        result *= 10;
        result += val % 10;
        val /= 10;
    }
    return result;
}


int main(void)
{
    unsigned x = 12321;

    printf("The reversed and not reversed are %s\n", x == reverse(x) ? "equal" : "not equal");
}
0___________
  • 60,014
  • 4
  • 34
  • 74
0

after extracting the last digit num%10, now the original number is reduced to single digit number hence it is not equal to the reversed number

apeksha
  • 1
  • 1
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 20 '23 at 16:14
-1
#include<stdio.h>
int main()
{
    int n,a,b,c,d,e,revnum=0;
    printf("Enter a 5 digit number:");
    scanf("%d",&n);
    e=n%10;
    revnum=revnum+e*10000;
    d=(n/10)%10;
    revnum=revnum+d*1000;
    c=(n/100)%10;
    revnum=revnum+c*100;
    b=(n/1000)%10;
    revnum=revnum+b*10;
    a=(n/10000);
    revnum=revnum+a*1;
    printf("The reversed number is:%d",revnum);
    if(n==revnum){
        printf("\nThey are same.");
    }
    else{
        printf("\nThey are different.");
    }
    return 0;
}
  • Consider explaining why the code solves the problem. – Tilman Zuckmantel Mar 01 '22 at 19:33
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 01 '22 at 19:33