Harshad Number Or Not In Java Program
A Harshad number (or Niven number) is an integer that is divisible by the sum of its digits. For example, 153 is a Harshad number because the sum of its digits (1 + 5 + 3 = 9) divides 153 evenly (153 % 9 == 0). In this article, you will learn how to determine if a given number is a Harshad number using a Java program.
Problem Statement
The task is to write a Java program that takes an integer as input and checks whether it is a Harshad number. This involves calculating the sum of its digits and then performing a divisibility check.
Example
If the input number is 156, the sum of its digits is 1 + 5 + 6 = 12. Since 156 % 12 == 0, 156 is a Harshad number.
Background & Knowledge Prerequisites
To understand this article, you should have a basic understanding of:
- Java syntax (variables, loops, conditional statements).
- Arithmetic operators (
%for modulo,/for division,+for addition). - Input/output operations using
Scanner.
Use Cases or Case Studies
- Number Theory Education: Illustrating properties of numbers in a programming context.
- Coding Challenges: A common problem in introductory programming contests.
- Algorithm Practice: Good for practicing digit manipulation and basic conditional logic.
Solution Approaches
Approach 1: Iterative Digit Sum Calculation
This approach involves iteratively extracting each digit of the number, summing them up, and then checking for divisibility.
- One-line summary: Calculate the sum of digits using a
whileloop and then check if the original number is divisible by this sum.
// Harshad Number Checker
import java.util.Scanner;
// Main class containing the entry point of the program
public class Main {
public static void main(String[] args) {
// Step 1: Create a Scanner object to read user input
Scanner scanner = new Scanner(System.in);
// Step 2: Prompt the user to enter a number
System.out.print("Enter a number to check if it's a Harshad number: ");
int number = scanner.nextInt();
// Step 3: Store the original number for the divisibility check
int originalNumber = number;
int sumOfDigits = 0;
// Step 4: Calculate the sum of digits
// Use a while loop to extract digits until the number becomes 0
while (number > 0) {
int digit = number % 10; // Get the last digit
sumOfDigits += digit; // Add the digit to the sum
number /= 10; // Remove the last digit
}
// Step 5: Check if the original number is divisible by the sum of its digits
if (sumOfDigits != 0 && originalNumber % sumOfDigits == 0) {
System.out.println(originalNumber + " is a Harshad number.");
} else {
System.out.println(originalNumber + " is not a Harshad number.");
}
// Step 6: Close the scanner
scanner.close();
}
}
- Sample output:
Enter a number to check if it's a Harshad number: 156
156 is a Harshad number.
- Stepwise explanation:
- Initialize a
Scannerto get input from the user. - Read the integer input and store it in
number. - Create a copy of the
numberinoriginalNumberbecausenumberwill be modified during digit sum calculation. - Initialize
sumOfDigitsto0. - Enter a
whileloop that continues as long asnumberis greater than0.
- Inside the loop,
number % 10extracts the last digit. - This digit is added to
sumOfDigits. -
number /= 10removes the last digit fromnumber, effectively shifting the number to the right.
- After the loop, check if
sumOfDigitsis not zero (to avoid division by zero) and iforiginalNumberis perfectly divisible bysumOfDigitsusing the modulo operator (%). - Print the appropriate message indicating whether the number is a Harshad number.
- Close the
Scannerto prevent resource leaks.
Conclusion
Determining if a number is a Harshad number is a straightforward programming task that combines basic arithmetic operations with loop structures. The iterative approach of summing digits is efficient and easy to understand, making it suitable for this problem.
Summary
- A Harshad number is divisible by the sum of its digits.
- To check for a Harshad number:
- Calculate the sum of its digits.
- Check if the original number is perfectly divisible by this sum.
- The modulo operator (
%) and integer division (/) are key for digit extraction.