System.out.print("Enter a number : ");
int n = sc.nextInt();
int c, d, sum = 0,sum1=0,n1=0,count=0;
while(n>1)
{
c=n;
while(c>0)
{
d = c%10;
sum = sum + d;
c = c/10;
}
if(n%sum==0)
{
sum1=sum;
n1=n;
n=n/sum;
}
else
{
System.out.println("Not an harshad");
break;
}
}
if(n1%sum1==0)
System.out.println("Number is multiple harshad");
else
System.out.println("Number is not multiple harshad");
}
}
此代碼對於多個編號無法正常工作。它也給一個數字相同的輸出不是多個harshad.when我輸入108它給多個harshad,但是當我輸入8時它也給多個harshad數字。雖然圖8是不是多個哈沙德數多個Harshad編號
樣品輸入:6804
答案:
6 + 8 + 0 + 4 = 18 =>一十八分之六千八百零四= 378
378 => 3 + 7 + 8 = 18 = >一十八分之三百七十八= 21
21 => 2 + 1 = 3 => 21/3 = 7
輸入:126 輸出:126未哈沙德數
WAP在java中輸入一個號碼,並檢查數量爲多個harshad與否。據說多個harshad除以數字的總和後,會產生另一個Harshad數字。 –
請修改您的問題,以在問題本身中包括輸入,預期輸出和實際輸出,而不是標題(這只是您問題的簡短摘要)。任何關鍵的或重要的或相關的信息應該在問題本身中。 –
一個數字被稱爲Harshad,如果它可以被數字總和整除。 例如102可被3整除。 該商數不是Harshad,因爲34不能被7整除。 108是一個多Harshad數字,因爲該過程在1結束: 108/9 = 12; 12/3 = 4;四分之四= 1。 –