Java Program to Check Harshad Number

You are currently viewing Java Program to Check Harshad Number

Java program to check Harshad Number

package IncludeHelp;

import java.util.Scanner;

public class CheckHarshadNumber 
{
	public static void main(String args[])
    {
		// create object of the class.
        Scanner sc = new Scanner(System.in);
         
        // enter number here.
        System.out.print("Enter the number to check : ");
        int n = sc.nextInt();
        int c = n, d, sum = 0;
         
        //finding sum of digits
        while(c>0)
        {
            d = c%10;
            sum = sum + d;
            c = c/10;
        }
         
        // condition for harshadNumber
        if(n%sum == 0)
            System.out.println(n+" is a Harshad Number.");
        else
            System.out.println(n+" is not a Harshad Number.");      
    }
}

Output

First run:
Enter the number to check : 442
442 is not a Harshad Number.

Second run:
Enter the number to check : 18
18 is a Harshad Number.

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply