[6069 views]

A number given in any base is said to be a Niven number if it is divisible by the sum of its digits in that same base. In simple terms, a Niven number is a number that is divisible by the sum of its digits. Niven numbers are also known as Harshad numbers. An n-harshad number is an integer number divisible by the sum of its digit in base n. For example: 2, 3, 18, 45, 63, etc. are Niven numbers.

Let us consider the number 120:

Sum of the digits = 1+2+0 = 3
120 / 3 = 40
120 is divisible by 3.
Therefore, 120 is a Niven number.

Let us consider the number 145:

Sum of the digits = 1+4+5 = 10
145 / 10 = 14.5
145 is not divisible by 10.
Therefore, 145 is not a Niven number.

In this article, we will learn about the algorithm and flowchart to check whether a given number is Niven or Harshad number or not.

In this problem, we need to check whether a give number is a Niven number or not. To check this, we need to calculate the sum of the digits of the given number and check whether the number is divisible by that sum or not. If the number is divisible by that sum, it is a Niven number.

The algorithm starts off by taking the number to checked as user input, then the value is stored in a variable, say ‘n’. We create a copy of this number and store it in a variable say ‘temp’. This variable is required for doing the divisibility checking later. We now have to find out the sum of the digits. To do so, a while loop is started which runs until n is not equal to zero. We then extract the last digit of the number by performing: d = n % 10. After that, the extracted digit is added to the sum of the digits, ‘sum’ with the help of the statement: sum = sum + d. This loop continues iterating until no digits are left in n, that is, n is equal to zero.

Once this loop completes its execution, we get the sum of digits in ‘sum’. Now, we check whether temp is divisible by sum or not. If this checking returns true, we display “Niven number”, else, we display “Not Niven number”.

Note: Here ‘%’ is the modulus operator which returns the remainder value after division.

Let us consider an example:

Let n = 180

sum = 1+8+0 = 9

180 % 9 = 0

Hence, 180 is a Niven number.

- Java IDE Online
- Python IDE Online
- JSON Formatter/Minifier Online
- Case Converter
- Reverse String
- HTML Encoder
- HTML Decoder
- URL Encoder
- URL Decoder
- Decimal To Binary
- Binary To Decimal
- Decimal To Octal
- Binary To Octal
- Decimal To Hexadecimal
- Hexadecimal To Decimal
- Hexadecimal To Binary
- Octal To Decimal
- Octal to Hexadecimal
- Octal to Binary
- Calculate String Length
- Remove Spaces
- Remove Line Breaks
- Remove Empty Lines
- Remove Duplicate Lines
- Word Counter
- Replace Space with hyphen
- Check Armstrong number
- Text to URL

- Algorithm and flowchart to perform binary to octal conversion
- Flowchart and Algorithm for calculating X to the Power of Y i.e X
^{Y} - Algorithm and Flowchart to find if a Number is Even or Odd
- Algorithm and Flowchart to find the Maximum Absolute Difference Among the given Array
- Flowchart and Algorithm for Converting Infix to Postfix notation