# why do i always get true in the loop?

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 09-12-2008
C_ntua
Code:

```for (i=1; i <= numDigits/2; ++i) {   if ((num &#37; 10^i ) / 10^(i-1)  !=  (num / 10^(numDigits - i)) % 10^(i-1)) {       palindrome = 0;       break;   } }```
This is the code I suggested. It is short. Have no idea if it works. And you still need to count the digits. This can be done easily though:
Code:

```while (num !=0) {   num / 10;   ++numDigits; }```
Now reversing a number:
Code:

```temp = num; for (i=0; i <= numDigits; ++i) {   rev = 10^(numDigits-i) * (temp % 10);   temp /= 10; } if (rev != num)   palindrome = 0;```
NOTE that i haven't really paid attention to the bounds of the loop, the parenthesis and generally the details. Put you understand the idea. The second code is more readable, but a big longer. The first seems to be more slow in the worst case scenario, but it can break more quickly so it should give a better average perfomance (details, details I know)

EDIT: If you want to be even more strict, you could separate the number in two parts. If it is odd ignore the middle digit. Then reverse one part and check if the two parts are now equal. If they are it is a palindrome and you have an algorithm that runs in half speed from the other
• 09-12-2008
Masterx
Quote:

Originally Posted by C_ntua
Code:

```for (i=1; i <= numDigits/2; ++i) {   if ((num &#37; 10^i ) / 10^(i-1)  !=  (num / 10^(numDigits - i)) % 10^(i-1)) {       palindrome = 0;       break;   } }```
This is the code I suggested. It is short. Have no idea if it works. And you still need to count the digits. This can be done easily though:
Code:

```while (num !=0) {   num / 10;   ++numDigits; }```
Now reversing a number:
Code:

```temp = num; for (i=0; i <= numDigits; ++i) {   rev = 10^(numDigits-i) * (temp % 10);   temp /= 10; } if (rev != num)   palindrome = 0;```
NOTE that i haven't really paid attention to the bounds of the loop, the parenthesis and generally the details. Put you understand the idea. The second code is more readable, but a big longer. The first seems to be more slow in the worst case scenario, but it can break more quickly so it should give a better average perfomance (details, details I know)

EDIT: If you want to be even more strict, you could separate the number in two parts. If it is odd ignore the middle digit. Then reverse one part and check if the two parts are now equal. If they are it is a palindrome and you have an algorithm that runs in half speed from the other

tanx alot. i will try it .by the way how could you manage to think like this!?
• 09-12-2008
matsp
Just to add to C_ntua's suggestion - there is no need to calculate the number of digits before you reverse the number. Just replace the for-loop with the while-condition of the calculate number of digits loop, and use multiply by 10 to form the reversed number, you just picked the lowest digit off, so you need to multiply it "reamaining number of digits" by 10. I'm not going to write the code out for you, since writing the code is how you learn how to write code. Copy'n'paste is something ANYONE can do.

Also, 10 ^ x in C does not do 10 to the power of x - it caculates 10 XOR x, which is a somewhat "random" sequence of numbers. [And in this case, we only need about 10 or 11 numbers to cover the whole range of 32-bit integers (for 64-bit integers it would be about 18 numbers), so providing an array of 10 to the power of X as integers in table form would be quite feasible]

As to "how do you figure this out" - it is a combination of basic understanding of how to deal with "number to digits" and general "how you solve problems" (as in, what method can you use to calculate a number backwards).

--
Mats
• 09-12-2008
Masterx
many tanx ... specially for your comprehensive and detailed explanation of what took place so far ...

many thanks .. by the way , is it normal to think more than 5 hours for such a simple assignment (finding the best way of implementing it ? or its just a waist of time and energy , and i should concentrate on more complex issues !

tanx again ...
• 09-12-2008
master5001
What is normal anyway? Besides, percentage-wise more time is spent on small bugs that may only exist because of a single typo than on larger concepts. For example: x == 6; Nowadays compilers will mention these things, but those used to take ages to notice. In any event, you are obviously new, and as you get a deeper understanding of what you are doing, you will spend less time thinking about "simple things."
• 09-12-2008
C_ntua
Quote:

Originally Posted by Masterx
many tanx ... specially for your comprehensive and detailed explanation of what took place so far ...

many thanks .. by the way , is it normal to think more than 5 hours for such a simple assignment (finding the best way of implementing it ? or its just a waist of time and energy , and i should concentrate on more complex issues !

tanx again ...

Depends on what you want to do. Many ppl post here about bugs they have. They get from a simple solution to the most detailed things you can think of in C. My point is that it is good to learn, you don't actually have to sit and make perfect the program if don't want to or more importantly don't need to
• 09-13-2008
Masterx