×

Java Programs

Java Practice

Java program to check Harshad Number

In this java program, we are going to check whether a given number is Harshad Number or not.
Submitted by Chandra Shekhar, on January 18, 2018

Given a number, we have to check that the entered number is Harshad Number or not.

A number is said to be Harshad Number if and only if the sum of entered number completely divides the entered number (into 10 bases).

Example:

    Input: 18
    Sum = 1 + 8
    Now, 18 are dividing by 9 completely.

Program to check whether the given number is Harshad Number or not in Java

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.

Java Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.