Saturday, February 11, 2017

Write a Java Program to display all Harshad /Niven Number between 1 to 100.

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 Niven
{
public static void main(String...alt)throws Exception
{
for(int i=1;i<=100;i++)
{
int count=0;
int n=i;
int rem=0,sum=0;
while(n!=0)
{
rem=n%10;
sum+=rem;
n/=10;
}
if(i%sum==0)
out.println(i);
}
}
}

Share this

0 Comment to "Write a Java Program to display all Harshad /Niven Number between 1 to 100."

Post a Comment