How To Check Whether a Number Is a Niven Number or Not in Java?



What is Niven Number?

A Niven number is a number that is completely divisible by the sum of its digits. For example, consider the number 20. If we calculate the sum of its digits, 2 + 0, we get 2. When we divide 20 by 2 (20 / 2), the remainder is 0 (which means it is completely divisible).

Here are some other examples of Niven numbers such as 1, 2, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, etc.

Input & Output Scenarios

The following input and output scenarios will give you an idea of how to check mathematically whether the number is a given number:

Scenario 1

Suppose the input number is 18:

Input: 18
Output: Yes
Calculation:
find each digit of 18, which is 1 and 8
calculate the sum of the digits, 1 + 8 = 9;
divide 18 by sum 9, 18 / 9 = 0 (remainder)

Since the number 18 is completely divisible by the sum of its digits, 18 is a Niven number.

Scenario 2

Suppose the given number is 14:

Input: 14
Output: No
Calculation:
find each digit of 14, which is 1 and 4
calculate the sum of the digits, 1 + 4 = 5;
divide 18 by sum 5, 14 / 5 = 4 (remainder)

Since the number 14 is not divisible by the sum of its digits, 14 is not a Niven number.

Example 1

The following example checks whether the number 21 is a Niven number. To determine this, we calculate the sum of its digits and then divide the number by this sum. If the number is completely divisible by the sum, then the number is a Niven number; otherwise, it is not:

public class checkNiven{
  public static void main(String args[]){
     int num = 21;
     System.out.println("The given number is: "+num);
     
     int copyOfnum = num;
     int sum = 0;
     
     while(num > 0){
        //get the last digit
        int rem = num % 10;
        //find the sum of the digits
        sum = sum + rem;
        //remove last digit after each iteration
        num = num/10;
     }
     
     //check if the number is divisble by sum
     if(copyOfnum % sum == 0){
        System.out.println("Yes! " + copyOfnum + " is a niven number");
     }
     else{
        System.out.println("No! " + copyOfnum + " is not a niven number");
     }
  }
}

Following is the output of the above program:

The given number is: 21
Yes! 21 is a niven number

Example 2

The following program converts a given number 41 into a string using the toString() method and accesses each element using the charAt() method by iterating over it.

Then, we subtract the character '0' from each character to get the actual numeric value (ASCII) of that character and calculate the sum of these values. If the original number is divisible by this sum, it is a Niven number, or else no:

public class checkNiven{
   public static boolean checkNivenNumber(int num){
      int copyOfNum = num;
      int sum = 0;
      
      //convert the integer to string
      String str = Integer.toString(num);
      int length = str.length();
      
      for(int i = 0; i < length; i++){
         sum += str.charAt(i) - '0';
      }
      
      if(copyOfNum % sum == 0){
         return true;
      }
      return false;
   }
   public static void main(String args[]){
      int num = 41;
      System.out.println("The given number is: " + num);
      
      //calling the checkNivenNumber() method to check niven number
      System.out.println("Is number " + num + " is a niven number: " + checkNivenNumber(num));
   }
}

The above program produces the following output:

The given number is: 41
Is number 41 is a niven number: false
Updated on: 2025-06-05T21:24:10+05:30

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements