Friday, February 10, 2017

Write a Java Program which takes a number and check whether it is a Harshad Number/Niven Number or not.

Harshad Number : In recreational mathematics, a Harshad number (or Niven number), is an integer
(in base 10) that is divisible by the sum of its digits.

Ex1:
The number 18 is a Harshad number in base 10, because the sum of the digits 1 and 8 is 9 (1 + 8 = 9)
and 18 is divisible by 9 (since 18 % 9 = 0). hence 18 is harshad no.

Ex2:-
The number 1729 is a Harshad number in base 10, because the sum of the digits 1 ,7, 2, 9 is
19 (1 + 7 + 2 + 9 = 19), and 1729 is divisible by 19 (1729 % 19 = 0)

Ex3:-
The number 19 is not a Harshad number in base 10, because the sum of the digits 1 and 9 is
10 (1 + 9 = 10), and 19 is not divisible by 10 (since 19 % 10 = 9)  */

import static java.lang.System.out;
import java.util.Scanner;
class Harshad
{
public static void main(String...alt)throws Exception
{
int count=0;
Scanner sc=new Scanner(System.in);
out.println("Enter a Number : ");
int n=sc.nextInt();
int temp=n;
int rem=0,sum=0;
while(n!=0)
{
rem=n%10;
sum+=rem;
n/=10;
}
if(temp%sum==0)
out.println(temp+" is Harshad No");
else
out.println(temp+" is not Harshad No");
}
}

Share this

0 Comment to "Write a Java Program which takes a number and check whether it is a Harshad Number/Niven Number or not."

Post a Comment