Showing posts with label program to find given number is harshad number or not. Show all posts
Showing posts with label program to find given number is harshad number or not. Show all posts

Thursday, 17 August 2017

HARSHAD NUMBER / NIVEN NUMBER

HARSHAD NUMBER (or) NIVEN NUMBER:


In recreational mathematics, a harshad number (or Niven number) in a given number base, is an integer that is divisible by the sum of its digits when written in that base. Harshad numbers in base n are also known as n-harshad (or n-Niven) numbers. 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. All integers between zero and n are n-harshad numbers.


Here some niven numbers are given they are:

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
 

Example:
Let’s take the number 126. First, we must find the sum of the digits.
1 + 2 + 6 = 9
Next, we try to divide 126 by 9. And we find…
126 ÷ 9 = 14
There it is. 14 and no remainder; or put another way: 9 is a factor of 126 because
126 = 9 × 14


/*Java program to find given number is Harshad number/ niven number or not*/


import java.util.*;
class HarshadNumber
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
         
        System.out.print("Enter a number : ");
        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;
        }
         
        if(n%sum == 0)
            System.out.println(n+" is a Harshad Number.");
        else
            System.out.println(n+" is not a Harshad Number.");      
    }
}


OUTPUT:
Enter a number : 123
123 is not a Harshad Number

Enter a number : 6
6 is a Harshad Number.