Find the largest palindrome made from the product of two 3-digit numbers C#












2















I tried to solve projecteuler 4th project with C# but I don't receive the correct answer, I get 90909. Can someone spot my mistake?



The problem goes like this:




A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.



Find the largest palindrome made from the product of two 3-digit numbers.




namespace Largest_palindrome_product{

class Program
{
static void Main(string args)
{
string Reverse(string s)
{
char charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}


int result = 0;
string rev= "hello";
string palindrome = "hello";
string bingo = "hello";
int j = 1;
for (int i = 1; i< 1000; i++)
{
for (int y = 1; y< 1000; y++)
{
result = i * y;
bingo = result.ToString();
rev = Reverse(bingo);
j = int.Parse(bingo);
}

if (rev == bingo)
{
palindrome = bingo;
}
}
Console.Write(palindrome);
Console.Read();
}
}


}










share|improve this question


















  • 2





    You're checking rev and bingo outside the inner loop.

    – John
    Nov 21 '18 at 15:56






  • 1





    I take it you're turning it into a string to easily reverse it? I'd keep it as an int and just compare the ints.

    – Matt
    Nov 21 '18 at 15:56






  • 2





    Why don't you try starting you loops at the MAX value and substract 1 each iteration. This way the first coincidence will be the maximum palindrome, not the first one. Also the if should be inside the inner loop as stated by @John.

    – r1verside
    Nov 21 '18 at 16:00








  • 1





    Ops, yep my mistake.

    – r1verside
    Nov 21 '18 at 16:02
















2















I tried to solve projecteuler 4th project with C# but I don't receive the correct answer, I get 90909. Can someone spot my mistake?



The problem goes like this:




A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.



Find the largest palindrome made from the product of two 3-digit numbers.




namespace Largest_palindrome_product{

class Program
{
static void Main(string args)
{
string Reverse(string s)
{
char charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}


int result = 0;
string rev= "hello";
string palindrome = "hello";
string bingo = "hello";
int j = 1;
for (int i = 1; i< 1000; i++)
{
for (int y = 1; y< 1000; y++)
{
result = i * y;
bingo = result.ToString();
rev = Reverse(bingo);
j = int.Parse(bingo);
}

if (rev == bingo)
{
palindrome = bingo;
}
}
Console.Write(palindrome);
Console.Read();
}
}


}










share|improve this question


















  • 2





    You're checking rev and bingo outside the inner loop.

    – John
    Nov 21 '18 at 15:56






  • 1





    I take it you're turning it into a string to easily reverse it? I'd keep it as an int and just compare the ints.

    – Matt
    Nov 21 '18 at 15:56






  • 2





    Why don't you try starting you loops at the MAX value and substract 1 each iteration. This way the first coincidence will be the maximum palindrome, not the first one. Also the if should be inside the inner loop as stated by @John.

    – r1verside
    Nov 21 '18 at 16:00








  • 1





    Ops, yep my mistake.

    – r1verside
    Nov 21 '18 at 16:02














2












2








2








I tried to solve projecteuler 4th project with C# but I don't receive the correct answer, I get 90909. Can someone spot my mistake?



The problem goes like this:




A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.



Find the largest palindrome made from the product of two 3-digit numbers.




namespace Largest_palindrome_product{

class Program
{
static void Main(string args)
{
string Reverse(string s)
{
char charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}


int result = 0;
string rev= "hello";
string palindrome = "hello";
string bingo = "hello";
int j = 1;
for (int i = 1; i< 1000; i++)
{
for (int y = 1; y< 1000; y++)
{
result = i * y;
bingo = result.ToString();
rev = Reverse(bingo);
j = int.Parse(bingo);
}

if (rev == bingo)
{
palindrome = bingo;
}
}
Console.Write(palindrome);
Console.Read();
}
}


}










share|improve this question














I tried to solve projecteuler 4th project with C# but I don't receive the correct answer, I get 90909. Can someone spot my mistake?



The problem goes like this:




A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.



Find the largest palindrome made from the product of two 3-digit numbers.




namespace Largest_palindrome_product{

class Program
{
static void Main(string args)
{
string Reverse(string s)
{
char charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}


int result = 0;
string rev= "hello";
string palindrome = "hello";
string bingo = "hello";
int j = 1;
for (int i = 1; i< 1000; i++)
{
for (int y = 1; y< 1000; y++)
{
result = i * y;
bingo = result.ToString();
rev = Reverse(bingo);
j = int.Parse(bingo);
}

if (rev == bingo)
{
palindrome = bingo;
}
}
Console.Write(palindrome);
Console.Read();
}
}


}







c#






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 15:51









GeorgeGeorge

132




132








  • 2





    You're checking rev and bingo outside the inner loop.

    – John
    Nov 21 '18 at 15:56






  • 1





    I take it you're turning it into a string to easily reverse it? I'd keep it as an int and just compare the ints.

    – Matt
    Nov 21 '18 at 15:56






  • 2





    Why don't you try starting you loops at the MAX value and substract 1 each iteration. This way the first coincidence will be the maximum palindrome, not the first one. Also the if should be inside the inner loop as stated by @John.

    – r1verside
    Nov 21 '18 at 16:00








  • 1





    Ops, yep my mistake.

    – r1verside
    Nov 21 '18 at 16:02














  • 2





    You're checking rev and bingo outside the inner loop.

    – John
    Nov 21 '18 at 15:56






  • 1





    I take it you're turning it into a string to easily reverse it? I'd keep it as an int and just compare the ints.

    – Matt
    Nov 21 '18 at 15:56






  • 2





    Why don't you try starting you loops at the MAX value and substract 1 each iteration. This way the first coincidence will be the maximum palindrome, not the first one. Also the if should be inside the inner loop as stated by @John.

    – r1verside
    Nov 21 '18 at 16:00








  • 1





    Ops, yep my mistake.

    – r1verside
    Nov 21 '18 at 16:02








2




2





You're checking rev and bingo outside the inner loop.

– John
Nov 21 '18 at 15:56





You're checking rev and bingo outside the inner loop.

– John
Nov 21 '18 at 15:56




1




1





I take it you're turning it into a string to easily reverse it? I'd keep it as an int and just compare the ints.

– Matt
Nov 21 '18 at 15:56





I take it you're turning it into a string to easily reverse it? I'd keep it as an int and just compare the ints.

– Matt
Nov 21 '18 at 15:56




2




2





Why don't you try starting you loops at the MAX value and substract 1 each iteration. This way the first coincidence will be the maximum palindrome, not the first one. Also the if should be inside the inner loop as stated by @John.

– r1verside
Nov 21 '18 at 16:00







Why don't you try starting you loops at the MAX value and substract 1 each iteration. This way the first coincidence will be the maximum palindrome, not the first one. Also the if should be inside the inner loop as stated by @John.

– r1verside
Nov 21 '18 at 16:00






1




1





Ops, yep my mistake.

– r1verside
Nov 21 '18 at 16:02





Ops, yep my mistake.

– r1verside
Nov 21 '18 at 16:02












1 Answer
1






active

oldest

votes


















2














I think what has caused so much confusion is the use of String this just complicates thing having to convert them back and forth.



Your program works fine (if the if is moved as per John's comment) if only you'd checked the new number was larger!



here is my take on it:



// stolen from https://www.geeksforgeeks.org/reverse-digits-integer-overflow-handled/
int Reverse(int num)
{
int rev_num = 0;
while (num > 0)
{
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}


int result = 0;
int palindrome = 0;
int j = 1;
for (int i = 999; i > 0; i--)
{
for (int y = 999; y > 0; y--)
{
result = i * y;
if (result == Reverse(result))
{
if (result > palindrome)
{
palindrome = result;
}
}
}
}
Console.Write(palindrome);
Console.Read();





share|improve this answer





















  • 2





    Before the second for-loop, you could add the following statement: if (i*999 <= palindrome) break; You cannot find a greater palindrome in that case, so you can stop immediately.

    – Stefan Illner
    Nov 21 '18 at 16:29













  • Nice call creating a decrescent loop

    – Kevin Kouketsu
    Nov 21 '18 at 17:17













  • @KevinKouketsu it was r1verside's idea! Also think you meant decrement not decrescent ?

    – Matt
    Nov 21 '18 at 17:18








  • 1





    @Matt I'm sorry, i'm Brazilian so you know... i`m always trying to write better. Thanks for the advice

    – Kevin Kouketsu
    Nov 21 '18 at 17:40











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415792%2ffind-the-largest-palindrome-made-from-the-product-of-two-3-digit-numbers-c-sharp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














I think what has caused so much confusion is the use of String this just complicates thing having to convert them back and forth.



Your program works fine (if the if is moved as per John's comment) if only you'd checked the new number was larger!



here is my take on it:



// stolen from https://www.geeksforgeeks.org/reverse-digits-integer-overflow-handled/
int Reverse(int num)
{
int rev_num = 0;
while (num > 0)
{
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}


int result = 0;
int palindrome = 0;
int j = 1;
for (int i = 999; i > 0; i--)
{
for (int y = 999; y > 0; y--)
{
result = i * y;
if (result == Reverse(result))
{
if (result > palindrome)
{
palindrome = result;
}
}
}
}
Console.Write(palindrome);
Console.Read();





share|improve this answer





















  • 2





    Before the second for-loop, you could add the following statement: if (i*999 <= palindrome) break; You cannot find a greater palindrome in that case, so you can stop immediately.

    – Stefan Illner
    Nov 21 '18 at 16:29













  • Nice call creating a decrescent loop

    – Kevin Kouketsu
    Nov 21 '18 at 17:17













  • @KevinKouketsu it was r1verside's idea! Also think you meant decrement not decrescent ?

    – Matt
    Nov 21 '18 at 17:18








  • 1





    @Matt I'm sorry, i'm Brazilian so you know... i`m always trying to write better. Thanks for the advice

    – Kevin Kouketsu
    Nov 21 '18 at 17:40
















2














I think what has caused so much confusion is the use of String this just complicates thing having to convert them back and forth.



Your program works fine (if the if is moved as per John's comment) if only you'd checked the new number was larger!



here is my take on it:



// stolen from https://www.geeksforgeeks.org/reverse-digits-integer-overflow-handled/
int Reverse(int num)
{
int rev_num = 0;
while (num > 0)
{
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}


int result = 0;
int palindrome = 0;
int j = 1;
for (int i = 999; i > 0; i--)
{
for (int y = 999; y > 0; y--)
{
result = i * y;
if (result == Reverse(result))
{
if (result > palindrome)
{
palindrome = result;
}
}
}
}
Console.Write(palindrome);
Console.Read();





share|improve this answer





















  • 2





    Before the second for-loop, you could add the following statement: if (i*999 <= palindrome) break; You cannot find a greater palindrome in that case, so you can stop immediately.

    – Stefan Illner
    Nov 21 '18 at 16:29













  • Nice call creating a decrescent loop

    – Kevin Kouketsu
    Nov 21 '18 at 17:17













  • @KevinKouketsu it was r1verside's idea! Also think you meant decrement not decrescent ?

    – Matt
    Nov 21 '18 at 17:18








  • 1





    @Matt I'm sorry, i'm Brazilian so you know... i`m always trying to write better. Thanks for the advice

    – Kevin Kouketsu
    Nov 21 '18 at 17:40














2












2








2







I think what has caused so much confusion is the use of String this just complicates thing having to convert them back and forth.



Your program works fine (if the if is moved as per John's comment) if only you'd checked the new number was larger!



here is my take on it:



// stolen from https://www.geeksforgeeks.org/reverse-digits-integer-overflow-handled/
int Reverse(int num)
{
int rev_num = 0;
while (num > 0)
{
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}


int result = 0;
int palindrome = 0;
int j = 1;
for (int i = 999; i > 0; i--)
{
for (int y = 999; y > 0; y--)
{
result = i * y;
if (result == Reverse(result))
{
if (result > palindrome)
{
palindrome = result;
}
}
}
}
Console.Write(palindrome);
Console.Read();





share|improve this answer















I think what has caused so much confusion is the use of String this just complicates thing having to convert them back and forth.



Your program works fine (if the if is moved as per John's comment) if only you'd checked the new number was larger!



here is my take on it:



// stolen from https://www.geeksforgeeks.org/reverse-digits-integer-overflow-handled/
int Reverse(int num)
{
int rev_num = 0;
while (num > 0)
{
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}


int result = 0;
int palindrome = 0;
int j = 1;
for (int i = 999; i > 0; i--)
{
for (int y = 999; y > 0; y--)
{
result = i * y;
if (result == Reverse(result))
{
if (result > palindrome)
{
palindrome = result;
}
}
}
}
Console.Write(palindrome);
Console.Read();






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 17:09

























answered Nov 21 '18 at 16:13









MattMatt

476210




476210








  • 2





    Before the second for-loop, you could add the following statement: if (i*999 <= palindrome) break; You cannot find a greater palindrome in that case, so you can stop immediately.

    – Stefan Illner
    Nov 21 '18 at 16:29













  • Nice call creating a decrescent loop

    – Kevin Kouketsu
    Nov 21 '18 at 17:17













  • @KevinKouketsu it was r1verside's idea! Also think you meant decrement not decrescent ?

    – Matt
    Nov 21 '18 at 17:18








  • 1





    @Matt I'm sorry, i'm Brazilian so you know... i`m always trying to write better. Thanks for the advice

    – Kevin Kouketsu
    Nov 21 '18 at 17:40














  • 2





    Before the second for-loop, you could add the following statement: if (i*999 <= palindrome) break; You cannot find a greater palindrome in that case, so you can stop immediately.

    – Stefan Illner
    Nov 21 '18 at 16:29













  • Nice call creating a decrescent loop

    – Kevin Kouketsu
    Nov 21 '18 at 17:17













  • @KevinKouketsu it was r1verside's idea! Also think you meant decrement not decrescent ?

    – Matt
    Nov 21 '18 at 17:18








  • 1





    @Matt I'm sorry, i'm Brazilian so you know... i`m always trying to write better. Thanks for the advice

    – Kevin Kouketsu
    Nov 21 '18 at 17:40








2




2





Before the second for-loop, you could add the following statement: if (i*999 <= palindrome) break; You cannot find a greater palindrome in that case, so you can stop immediately.

– Stefan Illner
Nov 21 '18 at 16:29







Before the second for-loop, you could add the following statement: if (i*999 <= palindrome) break; You cannot find a greater palindrome in that case, so you can stop immediately.

– Stefan Illner
Nov 21 '18 at 16:29















Nice call creating a decrescent loop

– Kevin Kouketsu
Nov 21 '18 at 17:17







Nice call creating a decrescent loop

– Kevin Kouketsu
Nov 21 '18 at 17:17















@KevinKouketsu it was r1verside's idea! Also think you meant decrement not decrescent ?

– Matt
Nov 21 '18 at 17:18







@KevinKouketsu it was r1verside's idea! Also think you meant decrement not decrescent ?

– Matt
Nov 21 '18 at 17:18






1




1





@Matt I'm sorry, i'm Brazilian so you know... i`m always trying to write better. Thanks for the advice

– Kevin Kouketsu
Nov 21 '18 at 17:40





@Matt I'm sorry, i'm Brazilian so you know... i`m always trying to write better. Thanks for the advice

– Kevin Kouketsu
Nov 21 '18 at 17:40


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415792%2ffind-the-largest-palindrome-made-from-the-product-of-two-3-digit-numbers-c-sharp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy