Java ProgramsControl FlowCheck Number Harshad

Check Number Harshad in Java

beginner·  Control Flow  ·  Loops

Problem

A Harshad number (also called a Niven number) is a number that is evenly divisible by the sum of its own digits.

Given a number, determine whether it is a Harshad number.

Input
18
Output
18 is a Harshad number: true

Java Program

Java
public class HarshadNumberCheck { public static void main(String[] args) { int n = 18; int original = n; int digitSum = 0; while (n > 0) { digitSum += n % 10; // peel off the last digit n /= 10; } boolean isHarshad = original % digitSum == 0; System.out.println(original + " is a Harshad number: " + isHarshad); } }

Output

18 is a Harshad number: true

Core Logic

Summing the digits first, then checking whether that sum evenly divides the original number, tests the definition directly.

How It Works
  1. 1A while loop peels off each digit of n with n % 10, adding it into digitSum, while n /= 10 removes it.
  2. 2The original value is kept in original before the loop consumes n.
  3. 3Once every digit has been summed, original % digitSum == 0 checks whether the digit sum divides the number evenly.
For 18, the digit sum is 1 + 8 = 9, and 18 % 9 == 0, so it's reported as a Harshad number.
💡

Key Point: Every single-digit number is trivially a Harshad number, since a number always divides evenly by itself — the digit sum only becomes interesting once a number has two or more digits.

Complexity
Time Complexity: O(d)Space Complexity: O(1)

Why: The loop runs once per digit to build the sum, and only a couple of integer variables are kept regardless of how large n is.

Key Concepts

digit extractionmodulo operatordivisibility

Approach 2: Java 8

Java
public class HarshadNumberCheckStream { public static void main(String[] args) { int n = 18; // Maps each digit character to its numeric value and sums them int digitSum = String.valueOf(n).chars().map(c -> c - '0').sum(); boolean isHarshad = n % digitSum == 0; System.out.println(n + " is a Harshad number: " + isHarshad); } }

Output

18 is a Harshad number: true

Core Logic

The same digit sum can be produced by mapping each character of the number's string form to its numeric value and reducing the stream to a total.

How It Works
  1. 1String.valueOf(n).chars() returns an IntStream of the number's digit characters.
  2. 2.map(c -> c - '0') converts each character code to its actual digit value using ASCII arithmetic.
  3. 3.sum() reduces the stream of digits down to a single total, the same digit sum the loop computes.
For 18, the stream maps '1' and '8' to 1 and 8, summing to 9, the same digit sum as the loop version.
💡

Key Point: This expresses the exact same digit-summing logic as a stream pipeline instead of a manual loop — the divisibility check afterward is unchanged.

Complexity
Time Complexity: O(d)Space Complexity: O(1)

Why: The stream still visits each digit once to build the sum, and holds no more than a running total.

Key Concepts

Streamchars()IntStream.sum()

Related Programs