简体   繁体   English

两个三位数的乘积一个回文

[英]product of two 3 digit numbers a palindrome

I have written the following code for finding the first palindrome obtained by product of two 3 digit numbers:我编写了以下代码来查找由两个 3 位数字的乘积获得的第一个回文:

#include <stdio.h>

int main()
{
    int pro,d,sum=0,c;
    for (int a=100;a<1000;a++)
    {
        for (int b=a;b<1000;b++)
        {
            pro=a*b;
            d=pro;
            while (d!=0)
            {
                c=d%10;
                sum= sum*10 + c;
                d=d/10;
            }

            if (sum==pro)
            {
                printf("%d is a palindrome \n",sum);
                return 0;
            }
        }
   }
   return 0;
}

But when I run the code it doesn't give me any output.但是当我运行代码时,它没有给我任何输出。 Any helps?有帮助吗?

You need to reset the sum value after every iteration or inner for loop otherwise it used the previous iteration value in next subsequent iteration.您需要在每次迭代或内部 for 循环后重置总和值,否则它将在下一次后续迭代中使用前一次迭代值。

Code :代码 :

#include <stdio.h>

int main()
{
   int pro,d,sum=0,c;
 for (int a=100;a<1000;a++)
{
    for (int b=a;b<1000;b++)
    {
        pro=a*b;
        d=pro;
        while (d!=0)
        {
            c=d%10;
            sum= sum*10 + c;
            d=d/10;
        }
        if (sum==pro)
        {
            printf("%d is a palindrome \n",sum);
            return 0;
        }
        sum=0;
    }
}

    return 0;
}

Output :输出 :

10201 is a palindrome

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM