Q:

Java program to check Harshad Number

belongs to collection: Java Basic Programs

0

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.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to check Pronic Number... >>
<< Java program to check Evil number...