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


Niven number can be defined as a number which is divisible by the sum of its digits. It is also called as Harshad number.

In this article we will see how to check Niven numbers by using the Java programming language.

To show you some instances

Instance-1

Input number is 3

Let’s check it by using the logic of Niven number −

Sum of the digits of the number = 3.

So, 3 is divisible by 3.

Hence, 3 is a Niven number.

Instance-2

Input number is 18

Let’s check it by using the logic of Niven number −

Sum of the digits of the number = 1 + 8 = 9.

So, 18 is divisible by 9.

Hence, 18 is a Niven number.

Instance-3

Input number is 14

Let’s check it by using the logic of Niven number −

Sum of the digits of the number = 1 + 4 = 5.

So, 14 is not divisible by 5.

Hence, 14 is not a Niven number.

Some other examples of Niven numbers include 1, 2, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20 etc.

Syntax

To convert integer value to String value by using inbuilt toString() method.

Following is the Syntax to get total number of digits in a number by converting the integer value to String value and then finding its length and assigning the length to an integer variable −

String str = Integer.toString(input_number);

To get the length of the Integer, we will use the Java String class inbuilt length() method which returns the length of the String object.

int length = st.length();

To get the character at a specific position/index in a string we use the charAt() method. Where charAt(i)-‘0’ returns the actual integer value.

int num = st.charAt(i)-‘0’;

Where ‘st’ refers to the string ‘i’ is the iterator variable to iterate the string.

Algorithm

Algorithm 1

  • Step 1 − Get an integer number either by initialization or by user input.

  • Step 2 − By iterating each digit of the number, find the sum of each digit of the number.

  • Step 3 − Then check if the original number is divisible by the sum of all digits of the number. If divisible then the given number is a Niven number else it is not a niven number.

Algorithm 2

  • Step 1 − Get an integer number either by initialization or by user input.

  • Step 2 − Convert that integer to String by using inbuilt toString() method.

  • Step 3 − Find the length of the String by using the inbuilt length() method.

  • Step 4 − Then by using a for loop, iterate till the length of the String and get one by one integer value from String by using charAt(i)-‘0’ and keep a track on the sum of all digits.

  • Step 5 − Then check if the original number is divisible by the sum of all digits of the number. If divisible then the given number is a Niven number else it is not a niven number.

Multiple Approaches

We have provided the solution in different approaches.

  • Without Using String

  • By Using String

Let’s see the program along with its output one by one.

Approach-1: Without Using String

In this approach an integer value will be initialized in the program and then by using the Algorithm-1 we can check whether a number is a Niven number or not.

Example

import java.util.*; public class Main{ //main method public static void main(String args[]){ //declared an integer variable and initialized the value int originalNumber = 21; //print the given input number System.out.println("Given number:"+originalNumber); //keep a copy of original number int copyOfOriginalNumber = originalNumber; //initialize sum as 0 int sum = 0; //Find sum of all digits of the number //continue the loop till the number is greater than 0 while(originalNumber > 0){ //get the rightmost digit of the number by using % operator int rem = originalNumber%10; //add the digit(rem) to sum sum = sum + rem; //remove the rightmost digit from number and get the updated number originalNumber = originalNumber/10; } //printing the result if(copyOfOriginalNumber % sum == 0) System.out.println(copyOfOriginalNumber+" is a niven number"); else System.out.println(copyOfOriginalNumber+" is not a niven number"); } }

Output

Given number:21
21 is a niven number

Approach-2: By Using User Defined

In this approach an integer value will be initialized in the program then by using the Algorithm-2 we will check if the number is a niven number or not.

Example

import java.util.*; public class Main{ //main method public static void main(String args[]){ //declared an integer variable and initialized the value int originalNumber = 40; //print the given input number System.out.println("Given number:"+originalNumber); //keep a copy of original number int copyOfOriginalNumber = originalNumber; //initialize sum as 0 int sum = 0; //convert the integer to string by using toString() method String str = Integer.toString(originalNumber); //find length of String by using length() method //which is nothing but total number of digits in the given number int length=str.length(); //iterate the String and get the digits by using charAt(i)-'0' //find the sum of digits for(int i = 0; i < length; i++){ sum += str.charAt(i)-'0'; } //printing the result if(copyOfOriginalNumber % sum == 0) System.out.println(copyOfOriginalNumber+" is a niven number"); else System.out.println(copyOfOriginalNumber+" is not a niven number"); } }

Output

Given number:40
40 is a niven number

In this article, we explored how to check a number whether it is a niven number or not in Java by using different approaches.

Updated on: 27-Oct-2022

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements