Reputation: 23
I want to write a program which displays palindromes numbers between 1 and 10000, I wrote a script which displays if the number typed by the user is a palindrome or no but when I add the for loop it gives me false results My code:
#include<iostream>
using namespace std;
int main()
{
int num, reverse = 0, remainder, temp;
for(num=0;num<1000;num++){
temp = num;
cout << "temp = " << temp << endl;
while( temp != 0)
{
remainder = temp % 10;
reverse = reverse * 10 + remainder;
temp = temp / 10;
cout << "remainder = " << remainder << endl;
cout << "reverse = " << reverse << endl;
cout << "temp = " << temp << endl;
}
cout << "Reversed number: " << reverse << endl;
if (num == reverse)
cout << "\n" << num << " is a palindrome number." << endl;
else
cout << "\n" << num << " is not a palindrome number." << endl;
}
return 0;
}
Upvotes: 0
Views: 1077
Reputation: 2260
In addition to the excellent codes other people have posted (I would go with those), this is another (and more readable) way in which you can do the same thing..
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
bool isPalindrome(unsigned number) {
std::vector<unsigned> number_as_vec;
while (number) {
number_as_vec.push_back(number % 10);
number /= 10;
}
for (size_t i = 0, j = number_as_vec.size() - 1; i < j; i++, j--) {
if (number_as_vec.at(i) != number_as_vec.at(j)) {
return false;
}
}
return true;
}
int main(int argc) {
unsigned num = 1;
while (num != 10001) {
if (isPalindrome(num)) {
std::cout << num << "\n";
}
num++;
}
return 0;
}
Also, instead of using vectors, one can simply use a string here:
bool isPalindrome(unsigned number) {
std::string num_as_string(std::to_string(number));
for (size_t i = 0, j = number_as_string.size() - 1; i < j; i++, j--) {
if (number_as_string.at(i) != number_as_string.at(j)) {
return false;
}
}
return true;
}
Upvotes: 0
Reputation: 222
I checked your program and noticed a few flaws:
1) All 1-digit numbers are Palindrome because their is reverse is same.
---Your program didn't display them as palindromes.
2) You don't have to display all the details like their remainder and reverse.
---Obviously the Palindromes reverse will be the same as the original. We don't need to display non-palindromes. In some cases reverse was displaying garbage values as well. I don't find a reason that will be of any help to the user.
Solution:-
#include<iostream>
using namespace std;
bool findPalindrome(const int);
int main()
{
for (int i = 0; i < 10000; i++) {
if (findPalindrome(i)) {
cout << "Number " << i << " is a Palindrome!" << endl;
}
}
return EXIT_SUCCESS; // Return EXIT_SUCCESS is my Specialty :D
}
bool findPalindrome(const int num) {
int temp = num;
int reve = 0;
while (temp != 0) {
reve = (reve * 10) + (temp % 10);
temp /= 10;
}
return (reve == num);
}
Upvotes: 1
Reputation: 22354
You need to make sure that reverse
is always 0
at the start of loop iteration:
for(num=0;num<1000;num++){
reverse = 0;
temp = num;
cout << "temp = " << temp << endl;
while( temp != 0)
{
remainder = temp % 10;
reverse = reverse * 10 + remainder;
temp = temp / 10;
cout << "remainder = " << remainder << endl;
cout << "reverse = " << reverse << endl;
cout << "temp = " << temp << endl;
}
cout << "Reversed number: " << reverse << endl;
if (num == reverse)
cout << "\n" << num << " is a palindrome number." << endl;
else
cout << "\n" << num << " is not a palindrome number." << endl;
}
Managing variables becomes much easier if you declare them where you need them instead of lumping everything together:
int main()
{
for(int num=0;num<1000;num++){
int reverse = 0;
int temp = num;
int remainder = 0;
cout << "temp = " << temp << endl;
while( temp != 0)
{
remainder = temp % 10;
reverse = reverse * 10 + remainder;
temp = temp / 10;
cout << "remainder = " << remainder << endl;
cout << "reverse = " << reverse << endl;
cout << "temp = " << temp << endl;
}
cout << "Reversed number: " << reverse << endl;
if (num == reverse)
cout << "\n" << num << " is a palindrome number." << endl;
else
cout << "\n" << num << " is not a palindrome number." << endl;
}
return 0;
}
Now you are sure remainder
is always initialized at the start of for
loop iteration and it cannot live longer than one iteration (for example, it won't live to the next iteration).
Upvotes: 1