+ 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; }
11 Respuestas
+ 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
+ 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
+ 3
reversed has not been initialised to 0 in the beginning
+ 3
Ya I corrected it after realising logic!
Thx:)Infinity
+ 2
Learner🎖
And also there should be double equal (==) for comparison
Single equal (=) use for assignment.
+ 2
I realised there is another error: It should be n=n/10
+ 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
+ 1
Thank you!
Can you help me understand the logic plz?🅰🅹 🅐🅝🅐🅝🅣 The future is now thanks to science
+ 1
Learner🎖 👍
0
You have a lots of mistakes