My code is really ugly, but I don't know how to make it better.
#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
bool palindromeTest(double numberVectorAddition){//tests if the factors of the palindrome are both below 3 digits each.
double p = numberVectorAddition;
double i = 999;
double c;
for (;; i--){
if (fmod(p, i) != 0)
continue;
if (fmod(p, i) == 0)
c = p / i;
if (1000 <= c){
cout << "One of the factors is too high. Looking for next largest palindrome..." << endl;
cout << endl;
return false;
}
else if (c <= 1000){
cout << "Factors of the number both below 1000 are " << i << " and " << c;
return true;
}
}
}
void nLargestPalindrome(double number){
int i = 5; //subtract "1" because array starts at zero
int intermediate = i; //intermediate value - equal to the array index;
double *decimalDigit = new double[i + 1];
while (0 <= i){ //acquire digits of number
decimalDigit[i] = (number - fmod(number, pow(10, i))) / pow(10, i);
cout << "Value of digit place " << i << " is " << decimalDigit[i] << endl;
number = fmod(number, pow(10, i));
i--;
}
for (int q = 0; q < 100; q++){ //decreases size of palindrome, and also tests after the decrease.
double numberVectorAddition = pow(10, 5)*decimalDigit[0] + pow(10, 4)*decimalDigit[1] + pow(10, 3)*decimalDigit[2] + pow(10, 2)*decimalDigit[3] + pow(10, 1)*decimalDigit[4] + decimalDigit[5];
cout << decimalDigit[0] << decimalDigit[1] << decimalDigit[2] << decimalDigit[3] << decimalDigit[4] << decimalDigit[5] << '\t';
cout << numberVectorAddition << endl;
if (palindromeTest(numberVectorAddition))
break;
decimalDigit[2]--;
decimalDigit[3]--;
if (decimalDigit[2] == 0){
numberVectorAddition = pow(10, 5)*decimalDigit[0] + pow(10, 4)*decimalDigit[1] + pow(10, 3)*decimalDigit[2] + pow(10, 2)*decimalDigit[3] + pow(10, 1)*decimalDigit[4] + decimalDigit[5];
cout << decimalDigit[0] << decimalDigit[1] << decimalDigit[2] << decimalDigit[3] << decimalDigit[4] << decimalDigit[5] << '\t';
cout << numberVectorAddition << endl;
if(palindromeTest(numberVectorAddition))
break;
if ((decimalDigit[1] == 0) && (decimalDigit[2] == 0)){
decimalDigit[1] = 9;
decimalDigit[4] = 9;
decimalDigit[2] = 9;
decimalDigit[3] = 9;
decimalDigit[0]--;
decimalDigit[5]--;
continue;
}
decimalDigit[2] = 9;
decimalDigit[3] = 9;
decimalDigit[1]--;
decimalDigit[4]--;
}
}
delete decimalDigit;
}
int main(){
double n = 997799; //997799 is equal to 999*999 - 2; palindrome is smaller.
nLargestPalindrome(n); //finds the largest palindrome below "n" that has both factors of three digits.
cin.get(); //stops the command window from closing prematurely
}
//finds the largest palindrome below "n" that has both factors of three digits.
is better than the current title. Also, you might want to quickly explain the algorithm you have applied. \$\endgroup\$