Skip to content

[FEATURE]: Add Harshad numbers #1802

Description

@ridge-kimani

Motivation

  • A Harshad number (also known as a Niven number) is an integer that is divisible by the sum of its digits.
  • Harshad numbers are sometimes called “happy numbers” in recreational mathematics, because they have a simple, satisfying property: their digit sum divides them evenly. They are widely studied in number theory and often appear in math puzzles, programming challenges, and teaching exercises.

Examples

18 is a Harshad number because 1 + 8 = 9 and 18 % 9 === 0.

19 is not, because 1 + 9 = 10 and 19 % 10 !== 0

Possible workarounds

No response

Additional information

No response

Activity

changed the title [-][FEATURE]:[/-] [+][FEATURE]: Add Harshad numbers[/+] on Sep 24, 2025

Randomlyclueless commented on Oct 25, 2025

@Randomlyclueless

Hi! I noticed that the previous PR (#1801) was closed without merging. I’d love to take up this issue and implement the Harshad number checker for Hacktoberfest 2025. Could you please assign it to me?

added a commit that references this issue on Oct 26, 2025
5247a1d

Randomlyclueless commented on Oct 26, 2025

@Randomlyclueless

I have worked on the Harshad number checker and created a pull request with the implementation and tests. Please consider my contribution for this issue.

sharaddh commented on Jun 5, 2026

@sharaddh

function isHarshad(num) {
if (num <= 0) return false; // Harshad numbers are typically positive integers

// Convert number to string to iterate over digits, then sum them
const sumOfDigits = String(num)
    .split('')
    .reduce((sum, digit) => sum + parseInt(digit, 10), 0);

// Check divisibility
return num % sumOfDigits === 0;

}

// Test cases
console.log(isHarshad(18)); // true
console.log(isHarshad(19)); // false
console.log(isHarshad(1)); // true
console.log(isHarshad(10)); // true
console.log(isHarshad(12)); // true (1+2=3, 12/3=4)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions