Home » Java programs » Java basic programs

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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.