Harshad Number/Niven Number Demystified (With Simple Java Code)- For Beginners

INTRODUCTION

Harshad Number or Niven Number is a positive integer that is divisible by the sum of its digits. Harshad numbers were defined by D.R. Kaprekar, a mathematician from India. The word “Harshad” comes from the Sanskrit harṣa (joy) + da (give), meaning joy-giver. The term “Niven number” arose from a paper delivered by Ivan M. Niven at a conference on number theory in 1977.

For Example:
12–  Sum of Digits= 3;
12/3=4 and 12%3=0

The modulo function (%) returns the remainder after division.
Read more about the modulo function (%)Click Here.

198– Sum of Digits= 18
198/18=11 and 198%18=0

For in-depth Mathematical information regarding the nature of the Harshad/Niven Number, please- Click Here.


JAVA CODE

public class HarshadNivenNumber
{
public static void main(int n)
{
int x=n;                          //Creating a copy of the number
int sum=0;                    //Variable to calculate sum of digits
while(n!=0)
{
int a=n%10;                  //Extracting the Digit
sum=sum+a;               //Calculating the sum of Digits
n=n/10;                        //Reducing the number of Digit by 1 (removing the unit Digit)
}
if(x%sum==0)             //Condition of Harshad/Niven Number
System.out.println (“Yes, A Harshad/Niven Number”);
else
System.out.println (“Not a Harshad/Niven Number”);
}
}


LIST OF HARSHAD/NIVEN NUMBERS

1,2,3,4,5,6,7,8,9,10,12,18,20,21,24,27,30,36,40,42,45,48,50,54,60,63,70,72,80,81,84,90,100, 102,108,110,111,112,114,117,120,126,132,133,135,140,144,150,152,153,156,162,171,180,190 192,195,198,200,201,204,207……. and so on.


INTERESTING TASK

Find the number of Harshad/Niven numbers between 1 and 1000.

Answer- 213

 

 

 

 

 

Blog at WordPress.com.

Up ↑

Design a site like this with WordPress.com
Get started