Showing posts with label HARSHAD Number. Show all posts
Showing posts with label HARSHAD Number. Show all posts

Thursday, 18 July 2024

NIVEN Number - Java

 



A NIVEN or HARSHAD number is a number which is divisible by the sum of its digits.

Example:

18 is a NIVEN number

1+8 = 9

18%9 = = 0

Let's start the code of determining if a number is NIVEN or not.

Below is the code for determining if a number is NIVEN.

import java.util.Scanner;
class SUPERSPY
{
    public static void main(String[]args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number");
        int n = sc.nextInt();
        int sum = 0,temp = n;
        while(temp!=0)
        {
            sum+=temp%10;
            temp/=10;
        }
        if(n%sum == 0)
        {
            System.out.println("It is a NIVEN number");
        }
        else
        {
            System.out.println("It is not a NIVEN number");
        }
    }
}

Demonstration:

1. First, Scanner class is imported.
2. A class namely NIVEN have been defined.
3. A main method have been defined.
4. Object of Scanner class have been created.
5. A Number has been demanded from user by giving a message.
6. To accept and store the number, a variable namely n has been defined of int data type

7. Then, two different variables namely
    a) sum - This variable will store the sum of digits. First, it is initialized with 0.
    b) temp - This variable will hold the number given by user on temporary basis. Actually, entire operation will be done with temp variable. temp = n means the value stored in n namely variable is getting assigned to temp. It means that throughout the entire operation, original value stored in n namely variable will remain safe.

Now, let us see the working:

while(temp!=0)

This loop will continue its iteration until value of temp doesn't become equals to 0. Say for example, user gave 18. Here, first 8 will be extracted and at last, 1 will get extracted as it is in index 0. Refer to the below diagram for clear understanding.


sum+ = temp%10;

Here, we need to understand few things.

First, individual digits from given number is getting extracted using temp%10.
As in case of 18, the first extracted number using temp%10 will be 8.
Then, this number will get added to the variable sum.
At last, second number using temp%10 will be 1 and it will also be added to sum.

Now, sum is containing 8+1 = 9


temp/=10;

In previous operation, we have seen that the first extracted number using temp%10 was 8. Immediately, after this operation, temp/=10; will be executed which means 18 is now going to be divided by 10 resulting to 1.8. This operation will permanently delete this 8 from temp variable. so that in next iteration, 8 doesn't gets added to the sum variable again. Like this, it will keep on deleting the digits from temp variable.


Here is the final outcome:

sum is now containing sum of digits i.e. 1+8 = 9
n is containing 18

Finally, with the conditional statement using if-else, it is determined if n mod(%) sum is equal to 0 or not as it can be seen in the program. If n mod(%) sum is equal to 0, then it is displaying that the number is a NIVEN number. Otherwise, it is displaying that the number is not a NIVEN number.

Now, write this program and test it with various numbers and increase your knowledge.

Happy Coding.......