0

I am a beginner in C programming.
I coded finding whether a number is palindrome or not.
It is not working, so anybody help me

#include<stdio.h>
int sum=0,n,r;
int reverse(int x);
int main(){
   
    printf("the number you want to check whether it is palindrome or not ");
    scanf("%d",&n);
    reverse(n);
  
    if (sum==n)
    {
        printf("congratulations you got your palindrome number\n");
        
    }
    else{
        printf("sorry it is not palindrome number\n");
    }
    return 0;
}
int reverse(int x){
    while (n>0)
    {
        r=n%10;
        sum=sum*10+r;
        n=n/10;
    }
    return sum;       
}
greybeard
  • 2,249
  • 8
  • 30
  • 66
dark
  • 21
  • 1
  • Does the reverse function work? – klutt Apr 10 '21 at 06:53
  • 6
    The problem with global variables is they are global. You pass a parameter to `reverse` and don't use it and return a value that you also don't use. The only outcome of that function is that `n == 0` which makes `if (sum==n)` never true. – Retired Ninja Apr 10 '21 at 06:53
  • 1
    The ninja says _you modify`n` in your function_. – Paul Ogilvie Apr 10 '21 at 07:22
  • As a beginner it would be advisable to learn to debug your own code. Run the program in a debugger and step through it line by line. Examine the program flow and variable values as it runs. – kaylum Apr 10 '21 at 07:34

2 Answers2

1

Let's not use global variables whole problems cause by them, instead we can use local variables. In reverse function you have this line:

n=n/10;

You are overwriting its value and then in main function you compare with sum:

if (sum==n)

At the end of the reverse function value of n is 0. Because n is in global scope.

Final code:

#include<stdio.h>

int reverse(int x);

int main(){
    int n = 0;

    printf("the number you want to check whether it is palindrome or not ");
    scanf("%d",&n);
    int sum = reverse(n);

    if (sum==n) {
        printf("congratulations you got your  palindrome number ");
    } else {
        printf("sorry it is not palindrome number");
    }
    return 0;
}

 int reverse(int x){
    int sum = 0, r = 0;
    
    while (x>0) {
        r=x%10;
        sum=sum*10+r;
        x/=10;
    }
    return sum; 
}  

Kanony
  • 509
  • 2
  • 12
0

#include<stdio.h>
int reverse(int x);
int main(){
int sum,n;
 printf("the number you want to check whether it is palindrome or not ");
 scanf("%d",&n);
 sum=reverse(n);

 if (sum==n)
 {
    printf("congratulations you got your  palindrome number ");
    
 }
 else{
    printf("sorry it is not palindrome number");
 }
 return 0;
 }

 int reverse(int x){
     int r,sum=0;
 while (x>0){
        r=x%10;
        sum=sum*10+r;
        x=x/10;
    }
 return sum; 
 }
 
  1. In main you called reverse(int) function and after work reverse(int) also returned an integer but you are not receiving returned value in any variable. And value of sum (global variable) is zero, that's why you are getting wrong output.

  2. In function reverse(int x), you named parameter x but you used n;

  • 1
    It is not at all clear that you present a stab at working code, followed by an enumeration of remarks about the code in the question: Please edit your post. While at it, unify code indentation and use of blank lines in `main()` and `reverse()`. – greybeard Apr 11 '21 at 12:18
  • (And check the points in time `sum` and `n` are zero using the original code, and be explicit whether you are referring to *before `reverse(n)`* or *after*.) – greybeard Apr 11 '21 at 12:41
  • `[it's] working` Great. If you review the edit suggestion, you should see someone think *n (global variable) is zero* with the original code - a matter of *before* or *after reverse*. – greybeard Apr 11 '21 at 15:36