java

find-harshad-numbers-range()

Parameters: int startRange, int endRange

Start and end of the range to check for Harshad numbers

Returns: List of Harshad numbers within the range (inclusively)

The function 'find-harshad-numbers-range' takes a range of numbers and returns all the Harshad numbers in that range. Harshad numbers are integers that are divisible by the sum of their digits.

loops
if-conditions
modulo operation
list
Medium dificulty

Crafting Java Code to Determine Harshad Numbers in a Range

Hello Programmer! Welcome to our blog post. Today, we'll walk you through an interesting function in Java, that will enable you to find Harshad numbers within a given range. This function might add a dimension to the ways you deal with numbers in programming. So, let's delve into the coding-world mysteries together and make this journey enriching.

Step 1: Understand the question

Our goal here is to define a function in Java that finds and returns the Harshad numbers within a given range. A Harshad number (or Niven number), named after Indian mathematician D. R. Harshad, is an integer that is divisible by the sum of its digits.

To program this, we will essentially have to do two things:

  • In a loop that checks each number in the given range, we will determine if it is a Harshad number.
  • If it is, we will add it to a list of Harshad numbers.

By iterating through the range and performing the above operations, we'll have a list of Harshad numbers.

for (int i = start; i <= end; i++) {
    if (isHarshad(i)) { list.add(i); }
}

Step 2: Create helper function to check Harshad number

We need a function that checks whether or not a number is a Harshad number. This function will need to sum the digits of the number, and then check whether the original number is divisible by this sum.

public static boolean isHarshad(int num) {
    int digitSum = 0, n = num;
    while (n > 0) {
        digitSum += n % 10;
        n /= 10;
    }
    return num % digitSum == 0;
}

Step 3: Find Harshad numbers in a range

Now we can use our helper function within the original loop, which will check each integer in the given range.

public static List<Integer> findHarshadNumbers(int start, int end) {
    List<Integer> list = new ArrayList<>();
    for (int i = start; i <= end; i++) {
        if (isHarshad(i)) { list.add(i); }
    }
    return list;
}

Step 4: Full program

Here is the full Java program based on the above parts.

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        System.out.println(findHarshadNumbers(1, 100));
    }

    public static List<Integer> findHarshadNumbers(int start, int end) {
        List<Integer> list = new ArrayList<>();
        for (int i = start; i <= end; i++) {
            if (isHarshad(i)) { list.add(i); }
        }
        return list;
    }

    public static boolean isHarshad(int num) {
        int digitSum = 0, n = num;
        while (n > 0) {
            digitSum += n % 10;
            n /= 10;
        }
        return num % digitSum == 0;
    }
}

# Step 5: Testing

After you've implemented a function, you should test it with several test cases to ensure it works as expected. You can add more test cases or modify the existing ones.

Conclusion

By creating a separate helper function to check if a number is Harshad, and calling that helper function inside a loop that checks each number in the given range, we were able to create a Java function that correctly identifies all Harshad numbers in a given range.

Learn function in:

Harshad numbers

Numbers divisible by the sum of its digits

Learn more

Mathematical principle

Harshad numbers (also known as Niven numbers) are numbers that are divisible by the sum of their digits. For example, the number `18` is a Harshad number because it is divisible by the sum of its digits, `1 + 8 = 9`. In this function, the principle used is iterating through a range of numbers and determining whether each number meets this criteria.

Learn more