hi! i'am a beginner as you can see, otherwise i probably wouldn't have such a stupid problem :) i have been seeking for a method how i could reverse a number for example 2345 to 5432 and check whether it is the same after the modification, read the same way from both ends. i have been trying to implement string and arrays but can't seem to figure out how to do it. help will be much appreciated. :)
well, that was my idea too :) but i can't figure out how to do it. i would need to write every digit in a different slot of that char array wouldn't i? 2 - array[0], 3 - array[1] and so far i haven't been able to do it. i you could be so kind and write an example of how it's done.
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <stdio.h>
#include <stdlib.h>
usingnamespace std;
int main()
{
int nProduct; // the product of those two number
int nRev = 0; // reversed version of the nProduct
for ( int nNum1 = 999; nNum1 > 800; nNum1--)
{
for ( int nNum2 = 999; nNum2 > 800; nNum2--)
{
nProduct = nNum1 * nNum2;
int nProductT = nProduct; // temporary product
div_t nRevDT;
int nRevT; // temporary reversed value
nRevT = 0;
while (nProductT != 0)
{
nRevT *= 10;
nRevDT = div (nProductT, 10);
nRevT += nRevDT.rem;
nProductT = nProductT % 10;
}
if ( nProduct == nRevT)
{
if (nRevT > nRev)
{
nRev = nRevT;
}
}
cout << "sub result : the reversed version of the number " << nProduct << " is " << nRevT << endl;
}
}
cout << "the largest palindrome made from the product of two 3-digit numbers : "
<< nRev << endl << endl;
system("pause");
return 0;
}
#include <iostream>
int main (int argc, constchar * argv[])
{
int x = 123456;
int y =0;
while (x > 0) {
y *= 10;
y += x % 10;
x /= 10;
}
std::cout << y << std::endl;
return 0;
}
#include <iostream>
usingnamespace std;
long reverse(long num, int base=10) {
long val=0;
while (num != 0) {
val *= base;
val += num % base;
num /= base;
}
return val;
}
int main(int argc, char **argv) {
int biggest=0;
for (int i=100; i<1000; i++) {
for (int j=i; j<1000; j++) {
long prod=i*j;
if (prod == reverse(prod)) {
cout << "found palindrome : " << prod << endl;
if (prod > biggest) {
biggest=prod;
}
}
}
}
cout << "biggest is " << biggest << endl;
return 0;
}
Note i : A 3 digit number is anything from 100 to 999
Note ii : If you've done 100*300 there's no point doing 300*100 hence starting the inner for loop @ i
Note iii : Depending on your platform the product of two 3 digit numbers may overflow with int hence use long.
Note iv : The reverse will work in any base. As an exercise, modify the program to find palindromes in hexadecimal and print them in hex too....
Note v : Doing this by reversing the string representation of the number is a cheat, and is also way slower and uses more memory.
ok, it seems that my code was more or less the same as what emyr666 wrote and finally i got my answer :D although i still need to learn how to do it with strings... just one thing, it worked when i changed this part with while () but it didn't work before with do while ()