Multiple Harshad Number


        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;sum=0;
               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");    
    }
}

Sample input: 6804 Ans: 6+8+0+4=18=>6804/18=378 378=> 3+7+8=18=>378/18=21 21=> 2+1=3 =>21/3=7
Input: 126 Output : 126 is not harshad number

No comments:

Post a Comment

any problem in any program comment:-

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...