The code below has stylistic flaws plus a bug or two. List as many flaws as you can.
public int palindromeCount (int start, int finish) {
int k = start;
int returnVal = 0;
while (k<finish) {
int temp = k;
int r = 0;
while (temp > 0) {
r = 10 * r + temp%10;
temp = temp/10;
}
if (r == k) {
returnVal++;
}
k++;
}
return returnVal;
}
Palindrome is basically a number that has the same value if reversed, such as 11. The code here needs to browse through a range and finally end up with the amount of palindromes within that range. I'm doing this to learn loops.
Here is my progress:
public class Counter{
public Counter(){
}
public int palindromeCount (int start, int finish) {
int returnVal = 0;
int temp = start;
while (start < finish) {
int reverse = 0;
while (temp != 0) {
reverse = 10 * reverse + temp % 10;
temp = temp/10;
}
if (temp == start) {
returnVal = returnVal + 1;
}
start = start + 1;
}
return returnVal;
}
}
I think you posted this before, as far as I've tested it, it works well.
public static int palindromeCount(int start, int finish) {
int k = start;
int returnVal = 0;
while (k <= finish) { // Changed to <= to consider "finish" too
int temp = k;
int r = 0;
while (temp > 0) {
r = 10 * r + temp % 10;
temp = temp / 10;
}
if (r == k) {
returnVal++;
}
k++;
}
return returnVal;
}