vendredi 14 août 2020

if statement inside of for loop not being executed

Writing a program to solve problem four of project euler: Find the largest palindrome made from the product of two 2-digit numbers. Heres my reprex:

#include <iostream>

int reverseNumber(int testNum)
{

    int reversedNum, remainder = 0;
    int temp = testNum;

    while(temp != 0)
    {
        remainder = temp % 10;
        reversedNum = reversedNum * 10 + remainder;
        temp /= 10;
    }

    return reversedNum;
}

int main()
{
    const int MIN = 100;
    int numOne = 99;
    int product = 0;

    for(int numTwo = 10; numTwo < 100; numTwo++)
    {
        product = numOne * numTwo;

        if (reverseNumber(product) == product)
        {
            int solution = product;
            std::cout << solution << '\n';
            return 0;
        }        
    }

    return 0;
}

My main thought process behind this is that the for loop will go through every number from 10 to 99 and multiply it by 99. My intended outcome is for it to print 9009 which is the largest palindrome with 2 factors of 2 digits. So what I think should happen here is the for loop will go from 10 to 99, and each loop it should go through the parameters of the if statement which reverses the number and sees if it equals itself. I've made sure it wasn't a compiler issue, as this is recurring between different compilers. The reverseNumber() function returns the proper number every time I've tested it, so that shouldn't be the problem, however this problem only occurs when the function is involved in the logical comparison. By this I mean if that even I set it equal to a variable and put the variable in the if parameters, the issue still occurs. I'm pretty much stumped. I just hope it's not some silly mistake as I've been on this for a couple days now.

1 commentaire:

  1. Herpes is a serious and recurring disease which can't be cured through drugs or injections by the American doctors but the best way to deal with Herpes is by taking natural herbs medicine for it,  I have read about DR JAMES the great herbalist doctor who cure me from herpes  with his powerful herbal medicine.  i contacted him to know how he can help me and he told me never to worry that he will help me with the natural herbs from God!after 2 days of contacting him, he told me that the cure has been ready and he sent it to me via UPS SPEED POST and it got to me after 3 days!i used the medicine as he instructed me (MORNING and EVENING) and i was cured!its really like a dream but i am so happy!   for the people suffering from the following diseases, Cancer, hypothyroidism, Herpes, COPD, HIV, Arthritis, Hpv, Infections,Liver Disease, Autoimmune Diseases, Parkinson's disease,Lupus and more should contact him for his herbal medicine because i am a living testimony and i was cured of herpes and his medicine is legit.  i sent him what he requested and he sent me his medicine which i took for 3 good weeks and today am out here with negative result. when i went for test i was so happy after going through his medication.   CONTACT DR JAMES FOR PERMANENT CURE  EMAIL:drjamesherbalmix@gmail.com CALL OR WHATSAPP HIM ON +2348152855846  

    RépondreSupprimer