
Java Program on Harshad Number
- Categories Java
Accept a number from user and print if given number is Harshad number or not.
A number is said to be the Harshad number if it is divisible by the sum of its digit.
For example, 156= 1 + 5 + 6 = 12.
Since 156 is divisible by 12. So, 156 is a Harshad number.
Ans.
import java.util.Scanner;
public class HarshadNumber
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter any number:");
int n= sc.nextInt();
int m = n;
int sum = 0;
while (n != 0) {
int d = n%10;
sum = sum + d;
n = n / 10;
}
if ( m%sum == 0)
System.out.println ("yes");
else
System.out.println ("no");
}
} You may also like

Single Linked List Programs in Java
28 August, 2021

Implementing Stack using Array in Java
28 August, 2021

Constructor Programs
3 July, 2021
