+ 1
What's wrong in scanf I can't find any problem can anyone tell me plz ??
#include <stdio.h> int isPalindrome (int num) { int reversed = 0; int originalNumber = num; while (num != 0) { reversed = reversed * 10 + num %10; num = num /10; } printf("the reversed number is %d\n", reversed); if (originalNumber == reversed) { return 1; } else { return 0; } } int main (){ printf("Enter any number to check if it is palindrome or not"); scanf ("%d",&number); if (isPalindrome (number)) { printf("This number is palindrome\n"); } else { printf("This number is not palin drome\n"); } return 0; }
3 Answers
+ 4
Hi Syed,
From what I understand the problem seems to be with the second parameter when you invoke the scanf function in main. Upon doing this, you are passing the address to a variable that doesn't exist, where was number instantiated in main?
To fix this, add the following:
int number;
Before invoking the call to the scanf function.
Some may say it is good to assign the value 0 to number, while I agree with that in most cases, I would say it is fine here because we are directly getting input and assigning said input to number straight after we instantiated it.
I would also recommend fixing up the strings that you supply to your printf statements in main, while they are syntactically correct, some might complain that they could be perceived better upon removing the chunk of whitespace in the middle of the string. I have put my edited version of your code below:
#include <stdio.h>
int isPalindrome (int num)
{
int reversed = 0;
int originalNumber = num;
while (num != 0)
{
reversed = reversed * 10 + num %10;
num = num /10;
}
printf("the reversed number is %d\n",
reversed);
if (originalNumber == reversed)
{
return 1;
}
else
{
return 0;
}
}
int main (){
int number;
printf("Enter any number to check if it is palindrome or not");
scanf ("%d",&number);
if (isPalindrome (number))
{
printf("This number is palindrome\n");
}
else
{
printf("This number is not palin drome\n");
}
return 0;
}
Have fun and remember to never give up :)
+ 1
Hii
Oo now I understand 'thank you so much' to clear my doubt ..
And your explanation was very good đ