SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Harshad Number Program in Java and Python

12 August 2015

Harshad Number program with algorithm, explanation, Java solution and simple Python solution for ICSE and ISC students.

Question:

Write a program to input a number and check whether it is a Harshad Number or Niven Number or not.

Harshad Number: A Harshad Number, also called a Niven Number, is a number which is divisible by the sum of its digits.

Examples: 18 is a Harshad Number because 1 + 8 = 9 and 18 is divisible by 9. 1729 is a Harshad Number because 1 + 7 + 2 + 9 = 19 and 1729 is divisible by 19. 19 is not a Harshad Number because 1 + 9 = 10 and 19 is not divisible by 10.

Algorithm:

Step 1: Start.

Step 2: Accept a number from the user.

Step 3: Store a copy of the number in another variable.

Step 4: Initialize sum to 0.

Step 5: Extract the last digit of the copy using modulus 10.

Step 6: Add the extracted digit to sum.

Step 7: Remove the last digit from the copy using integer division by 10.

Step 8: Repeat Steps 5 to 7 until the copy becomes 0.

Step 9: If the original number is divisible by sum, display that it is a Harshad Number; otherwise, display that it is not a Harshad Number.

Step 10: Stop.

Explanation:

A Harshad or Niven number is divisible by the sum of its digits. The program first separates the digits and adds them.

A copy of the original number is stored because the digit extraction process repeatedly divides the number by 10. Without the copy, the original value would be lost.

In each loop cycle, the last digit is obtained using modulus 10 and added to sum. The number is then divided by 10 to remove that last digit.

After all digits are processed, the original number is divided by the digit sum. If the remainder is 0, the number is a Harshad/Niven number; otherwise, it is not.

The digit loop destroys the working copy of the number by repeated division by 10, so the original number must be preserved separately. The final divisibility test always uses this preserved value.

A Harshad or Niven number is checked by comparing the number with the sum of its digits. The program extracts each digit using % 10, adds it to a running sum, and removes it using integer division by 10. A copy of the original number must be preserved because the working number becomes zero after extraction. After the digit sum is obtained, the original number is divided by that sum. If the remainder is zero, the number is divisible by its digit sum and is therefore Harshad.

Java Program:

Java
/**
* The class HarshadNumber accepts a number and checks whether it is
* a Harshad Number or Niven Number.
*
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.Scanner;

class HarshadNumber
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int n = sc.nextInt();

        /*
        * copy is used for extracting digits so that the original
        * number n remains available for the final divisibility test.
        */
        int copy = n;
        int sum = 0;

        /*
        * Extract each digit from right to left and add it to sum.
        */
        while(copy > 0)
        {
            int digit = copy % 10;
            sum = sum + digit;
            copy = copy / 10;
        }

        /*
        * A number is Harshad if it is exactly divisible by
        * the sum of its digits.
        */
        if(n % sum == 0)
        System.out.println(n + " is a Harshad Number");
        else
        System.out.println(n + " is not a Harshad Number");
    }
}

Equivalent Python Program:

Python
n = int(input("Enter a number: "))

# copy is used to extract digits without changing the original number.
copy = n
total = 0

# Extract each digit from right to left and add it to total.
while copy > 0:
    digit = copy % 10
    total = total + digit
    copy = copy // 10

# A number is Harshad if it is divisible by the sum of its digits.
if n % total == 0:
    print(n, "is a Harshad Number")
else:
    print(n, "is not a Harshad Number")

Output:

Enter a number: 195 195 is a Harshad Number Enter a number: 194 194 is not a Harshad Number Enter a number: 190 190 is a Harshad Number Enter a number: 111 111 is a Harshad Number

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →