[英]Convert a number to its digits in C
本練習要求檢查一個數字是否等於其數字的立方之和。
Example : 153 = 1+125+27= 1^3 + 5^3 + 3^3
但是當我制作 153 時,輸出是 152,你能幫幫我嗎
bool Number(double n)
{
double k=n , m , s=0 ;
while(k!=0)
{
m=k %10 ;
s=s+pow(m,3) ;
k=(k-m)/10 ;
}
if(s==n)
{
return true ;
}
else
{
return false ;
}
}
int main()
{
double n ;
printf("give n please : ") ;
scanf("%f", &n) ;
if(Number(n)==true)
{
printf("\nThe sum of the cubes of its digits is equal to itself !\n") ;
}
else
{
printf("\nThe sum of the cubes of its digits is not equal to itself !\n") ;
}
return 0;
}
k=(k-m)/10 ;
綜上所述, k
可能繞過0
變為負數。
while(k!=0)
{
m=k %10 ;
s=s+pow(m,3) ;
k=(k-m)/10 ;
}
您只需
while(k!=0)
{
m=k %10 ;
s=s+pow(m,3) ;
k=k/10 ;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.