[英]Largest palindrome product - program not running, written solution but couldn't understand the problem
I knew the code for finding out a palindrome in case of a given number, and I tried to generalize it for finding the largest palindrome made from the product of two 3-digit numbers like this :- 我知道在给定数字的情况下查找回文的代码,并且我尝试将其归纳为查找由两个这样的3位数字的乘积构成的最大回文:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,no,d,j,temp;
int sum=0;
clrscr();
for(i=1;i<1000;i++)
{
for(j=1;j<1000;j++)
{
sum=0;
no=i*j;
temp=no;
while(no>0)
{
d=no%10;
sum=sum*10 + d;
no=no/10;
}
if(sum==temp)
{
printf("number is Palindrome %d\n",sum);
}
}
}
getch();
}
But, I am not getting the solution, can anyone Help? 但是,我没有解决方案,有人可以帮忙吗?
int sum=0
should be inside the inner for loop, just before the line no=i*j
: For each new number, the value of the sum should be started from zero. int sum=0
应该在内部for循环内,紧接在no=i*j
行之前:对于每个新数字,总和的值应从零开始。 If it is placed at the very beginning, it will take the value of sum
generated from the previous no
for all coming no
. 如果将其放在最开始,它将采用从前一个no
生成的sum
的值来sum
所有no
。
If you place sum=0
inside the j
loop before while
loop then for each new combination of i*j
the sum will start from 0
and you will 100% get all the desired palindromes 如果你把sum=0
内部j
循环之前while
循环然后为每个新组合i*j
总和将启动0
,你将100%获得所有所需的回文
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.