Array-promo

Question

Program to determine whether a given number is a Harshad number.

(A number is said to be the Harshad number if it is divisible by the sum of its digit.)

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class HarshadNumber  
{  
    public static void main(String[] args) 
    { 
        int num = 0;  
        int rem = 0, sum = 0, temp=0;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a number");
        num=sc.nextInt();
         
          
        /*Make a copy of num and store it in variable temp*/  
        temp = num;  
          
        /*Calculates sum of digits*/  
        while(temp> 0)
        {  
            rem = temp%10;  
            sum = sum + rem;  
            temp = temp/10;  
        }  
          
        //Checks whether number is divisible by sum of digits  
        if(num%sum == 0)  
            System.out.println(num + " is a harshad number");  
        else  
            System.out.println(num + " is not a harshad number");  
    }  
}  

				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *