+ 2

Can anyone find out mistake in this program? And please help me understand the logic of this code!(solved)

#include <stdio.h> int main() { int n,orginal,reversed,reminder; printf("enter the number that you want to check whether a palindrome"); scanf("%d",&n); orginal=n; while(n!=0){ reminder=n%10; reversed=reversed*10+reminder; n/10; } if(orginal=reversed) printf ("it is a palindrome"); else printf ("it is not a palindrome"); return 0; }

2nd May 2021, 8:02 AM
Hemasri Kottapalli
Hemasri Kottapalli - avatar
11 Réponses
+ 5
Explanation with an example: reversed = 0 original = 1234 Inside the loop: 1. remainder = 4 reversed = 0*10+4 = 4 n=123 2. remainder = 3 reversed = 4*10+3 = 43 n=12 3. remainder = 2 reversed = 43*10+2 = 432 n=1 4. remainder = 1 reversed = 432*10+1 = 4321 n=0 5. n=0 exit loop 1234 != 4321 Therefore its not a palindrome
2nd May 2021, 8:10 AM
Abhishek Tandon
Abhishek Tandon - avatar
+ 4
Thank you so much! Now I understood the logic! I've been struggling to get this logic! Even my sir couldn't make it clear though he explained 3 times! Thatswhy I love this community:)Infinity
2nd May 2021, 8:14 AM
Hemasri Kottapalli
Hemasri Kottapalli - avatar
+ 3
reversed has not been initialised to 0 in the beginning
2nd May 2021, 8:03 AM
Abhishek Tandon
Abhishek Tandon - avatar
+ 3
Ya I corrected it after realising logic! Thx:)Infinity
2nd May 2021, 8:15 AM
Hemasri Kottapalli
Hemasri Kottapalli - avatar
+ 2
Learner🎖 And also there should be double equal (==) for comparison Single equal (=) use for assignment.
2nd May 2021, 8:04 AM
A͢J
A͢J - avatar
+ 2
I realised there is another error: It should be n=n/10
2nd May 2021, 8:12 AM
Abhishek Tandon
Abhishek Tandon - avatar
+ 2
#include <stdio.h> int main() { int n,orginal,reversed,reminder; printf("enter the number that you want to check whether a palindrome\n"); scanf("%d",&n); orginal=n; while(n!=0){ reminder=n%10; reversed=reversed*10+reminder; n=n/10; } if(orginal==reversed) printf ("it is a palindrome"); else printf ("it is not a palindrome"); return 0; } I rectified them and it's working now! Lemme know if I'm still doing any mistake with this code!Xusanov Bexruz
3rd May 2021, 5:59 AM
Hemasri Kottapalli
Hemasri Kottapalli - avatar
+ 1
Thank you! Can you help me understand the logic plz?🅰🅹 🅐🅝🅐🅝🅣 The future is now thanks to science
2nd May 2021, 8:08 AM
Hemasri Kottapalli
Hemasri Kottapalli - avatar
+ 1
2nd May 2021, 8:15 AM
Abhishek Tandon
Abhishek Tandon - avatar
0
You have a lots of mistakes
3rd May 2021, 5:54 AM
Xusanov Bexruz
Xusanov Bexruz - avatar