Posted on by Kalkicode
Code Number

Harshad Number

The given problem is about determining whether a given number is a Harshad Number or not. A Harshad Number (also known as a Niven Number) is a positive integer that is divisible by the sum of its digits. For example, the number 18 is a Harshad Number because the sum of its digits (1 + 8 = 9) divides 18 (18 ÷ 9 = 2).

Problem Statement

Given a positive integer number, we need to check whether it is a Harshad Number or not. To do this, we'll find the sum of the digits of the given number and check if the number is divisible by this sum.

Example

Let's take a few examples to understand the concept better:

  1. Number: 18

    • Sum of Digits: 1 + 8 = 9
    • Check: 18 % 9 = 0 (It is divisible by the sum of its digits)
    • Result: 18 is a Harshad Number.
  2. Number: 46

    • Sum of Digits: 4 + 6 = 10
    • Check: 46 % 10 = 6 (It is not divisible by the sum of its digits)
    • Result: 46 is not a Harshad Number.

Pseudocode

Let's write the pseudocode for the Harshad Number algorithm:

function digit_sum(number):
    sum = 0
    while number != 0:
        sum += number % 10
        number /= 10
    return sum

function is_harshad_number(number):
    sum_of_digits = digit_sum(number)
    if number % sum_of_digits == 0:
        return True
    else:
        return False

Algorithm Explanation

  1. The digit_sum function calculates the sum of the digits of the given number. It iterates through each digit of the number by taking the remainder of the number divided by 10 and adds it to the sum variable. Then, the number is divided by 10 to move to the next digit. This process continues until the number becomes zero.

  2. The is_harshad_number function takes a number as input and calculates the sum_of_digits using the digit_sum function. It then checks if the number is divisible by the sum_of_digits. If the remainder is zero, it returns True, indicating that the number is a Harshad Number. Otherwise, it returns False.

Code Solution

Here given code implementation process.

/*
  C Program 
+ Check if a given number is Harshad Number or not
*/
#include<stdio.h>
#include<math.h>

//Returning the sum of digit in given number
int digit_sum(int number)
{
  int sum = 0; 

  while (number!=0) 
  { 
    //sum of digit
    sum+=number%10;
    number/=10; 
  } 
  return sum;
}
void is_harshad_no(int number)
{
  //When given number digit sum  divisible remainder is zero?
  if(number % digit_sum(number) == 0)
  {
    //When Yes
    printf("%d Is an Harshad Number\n",number);
  }
  else
  {
    //When No
    printf("%d Is not an Harshad Number\n",number);
  }
 
}
int main(){

  //Test Case
  is_harshad_no(72);
  is_harshad_no(46);
  is_harshad_no(135);
  is_harshad_no(57);
 
  return 0;
}

Output

72 Is an Harshad Number
46 Is not an Harshad Number
135 Is an Harshad Number
57 Is not an Harshad Number
/*
 C++ Program
 Check if a given number is Harshad Number or not
*/
#include<iostream>

using namespace std;

class MyNumber {
    public:

        //Returning the sum of digit in given number
        int digit_sum(int number) {
            int sum = 0;
            while (number != 0) {
                //sum of digit
                sum += number % 10;
                number /= 10;
            }
            return sum;
        }
    //When given number digit sum  divisible remainder is zero
    void is_harshad_no(int number) {
        if (number % this->digit_sum(number) == 0) {
            //When Yes

            cout << number << " Is an Harshad Number\n";
        } else {
            //When No

            cout << number << " Is not an Harshad Number\n";
        }
    }
};
int main() {
    MyNumber obj;
    //Test Case
    obj.is_harshad_no(72);
    obj.is_harshad_no(46);
    obj.is_harshad_no(135);
    obj.is_harshad_no(57);
    return 0;
}

Output

72 Is an Harshad Number
46 Is not an Harshad Number
135 Is an Harshad Number
57 Is not an Harshad Number
/*
  Java Program
  Check if a given number is Harshad Number or not
*/
public class MyNumber {
 
  //Returning the sum of digit in given number
  public int digit_sum(int number)
  {
    int sum = 0; 

    while (number!=0) 
    { 
      //sum of digit
      sum+=number%10;
      number/=10; 
    } 
    return sum;
  }
  //When given number digit sum  divisible remainder is zero
  public void is_harshad_no(int number)
  {

    if(number % digit_sum(number) == 0)
    {
      //When Yes
     System.out.print(number+" Is an Harshad Number\n");
    }
    else
    {
      //When No
      System.out.print(number+" Is not an Harshad Number\n");
    }
   
  }
 
  public static void main(String[] args) {

    MyNumber obj = new MyNumber();

    //Test Case
    obj.is_harshad_no(72);
    obj.is_harshad_no(46);
    obj.is_harshad_no(135);
    obj.is_harshad_no(57);


  }
}

Output

72 Is an Harshad Number
46 Is not an Harshad Number
135 Is an Harshad Number
57 Is not an Harshad Number
/*
  C# Program
  Check if a given number is Harshad Number or not
*/
using System;
public class MyNumber {

    //Returning the sum of digit in given number
    public int digit_sum(int number) {
        int sum = 0;

        while (number != 0) {
            //sum of digit
            sum += number % 10;
            number /= 10;
        }
        return sum;
    }
    //When given number digit sum  divisible reMainder is zero
    public void is_harshad_no(int number) {

        if (number % digit_sum(number) == 0) {
            //When Yes
            Console.Write(number + " Is an Harshad Number\n");
        } else {
            //When No
            Console.Write(number + " Is not an Harshad Number\n");
        }

    }

    public static void Main(String[] args) {

        MyNumber obj = new MyNumber();

        //Test Case
        obj.is_harshad_no(72);
        obj.is_harshad_no(46);
        obj.is_harshad_no(135);
        obj.is_harshad_no(57);


    }
}

Output

72 Is an Harshad Number
46 Is not an Harshad Number
135 Is an Harshad Number
57 Is not an Harshad Number
# Python 3 Program
# Check if a given number is Harshad Number or not
class MyNumber :
    # Returning the sum of digit in given number
    def digit_sum(self, number) :
        sum = 0
        while (number != 0) :
            # sum of digit
            sum += number % 10
            number = int(number / 10)
        
        return sum
    
    # When given number digit sum  divisible remainder is zero
    def is_harshad_no(self, number) :
        if (number % self.digit_sum(number) == 0) :
            # When Yes
            print(number ," Is an Harshad Number")
        else :
            # When No
            print(number ," Is not an Harshad Number")
        
    

def main() :
    obj = MyNumber()
    # Test Case
    obj.is_harshad_no(72)
    obj.is_harshad_no(46)
    obj.is_harshad_no(135)
    obj.is_harshad_no(57)


if __name__ == "__main__":
    main()

Output

72 Is an Harshad Number
46 Is not an Harshad Number
135 Is an Harshad Number
57 Is not an Harshad Number
# Ruby Program 
# Check if a given number is Harshad Number or not
class MyNumber 
    # Returning the sum of digit in given number
    def digit_sum(number) 
        sum = 0
        while (number != 0) 
            # sum of digit
            sum += number % 10
            number /= 10
        end
        return sum
    end
    # When given number digit sum  divisible remainder is zero
    def is_harshad_no(number) 
        if (number % self.digit_sum(number) == 0) 
            # When Yes

            print(number ," Is an Harshad Number\n")
        else 
            # When No

            print(number ," Is not an Harshad Number\n")
        end
    end
end
def main() 
    obj = MyNumber.new()
    # Test Case
    obj.is_harshad_no(72)
    obj.is_harshad_no(46)
    obj.is_harshad_no(135)
    obj.is_harshad_no(57)
end
main()

Output

72 Is an Harshad Number
46 Is not an Harshad Number
135 Is an Harshad Number
57 Is not an Harshad Number
/*
 Scala Program
 Check if a given number is Harshad Number or not
*/
class MyNumber {
    //Returning the sum of digit in given number
    def digit_sum(value: Int): Int = {
        var sum: Int = 0;
        var number : Int = value;
        while (number != 0) {
            //sum of digit
            sum += number % 10;
            number = (number / 10).toInt;
        }
        return sum;
    }
    //When given number digit sum  divisible remainder is zero
    def is_harshad_no(number: Int): Unit = {
        if (number % this.digit_sum(number) == 0) {
            //When Yes
            print(s"$number Is an Harshad Number\n");
        } else {
            //When No
            print(s"$number Is not an Harshad Number\n");
        }
    }
}
object Main {
    def main(args: Array[String]): Unit = {
        var obj: MyNumber = new MyNumber();
        //Test Case
        obj.is_harshad_no(72);
        obj.is_harshad_no(46);
        obj.is_harshad_no(135);
        obj.is_harshad_no(57);
    }
}

Output

72 Is an Harshad Number
46 Is not an Harshad Number
135 Is an Harshad Number
57 Is not an Harshad Number
/*
  Swift 4 Program
  Check if a given number is Harshad Number or not
*/
class MyNumber {
    //Returning the sum of digit in given number
    func digit_sum(_ value: Int) -> Int {
        var sum: Int = 0;
        var number: Int = value;
        while (number != 0) {
            //sum of digit
            sum += number % 10;
            number /= 10;
        }
        return sum;
    }
    //When given number digit sum  divisible remainder is zero
    func is_harshad_no(_ number: Int) {
        if (number % self.digit_sum(number) == 0) {
            //When Yes
            print(number ," Is an Harshad Number");
        } else {
            //When No
            print(number ," Is not an Harshad Number");
        }
    }
}
func main() {
    let obj: MyNumber = MyNumber();
    //Test Case
    obj.is_harshad_no(72);
    obj.is_harshad_no(46);
    obj.is_harshad_no(135);
    obj.is_harshad_no(57);
}
main();

Output

72  Is an Harshad Number
46  Is not an Harshad Number
135  Is an Harshad Number
57  Is not an Harshad Number
<?php
/*
  Php Program
  Check if a given number is Harshad Number or not
*/
class MyNumber {
    //Returning the sum of digit in given number

    public  function digit_sum($number) {
        $sum = 0;
        while ($number != 0) {
            //sum of digit
            $sum += $number % 10;
            $number = intval($number / 10);
        }
        return $sum;
    }
    //When given number digit sum  divisible remainder is zero

    public  function is_harshad_no($number) {
        if ($number % $this->digit_sum($number) == 0) {
            //When Yes

            echo($number ." Is an Harshad Number\n");
        } else {
            //When No

            echo($number ." Is not an Harshad Number\n");
        }
    }
};

function main() {
    $obj = new MyNumber();
    //Test Case

    $obj->is_harshad_no(72);
    $obj->is_harshad_no(46);
    $obj->is_harshad_no(135);
    $obj->is_harshad_no(57);
}
main();

Output

72 Is an Harshad Number
46 Is not an Harshad Number
135 Is an Harshad Number
57 Is not an Harshad Number
/*
 Node Js Program
 Check if a given number is Harshad Number or not
*/
class MyNumber {
    //Returning the sum of digit in given number
    digit_sum(number) {
        var sum = 0;
        while (number != 0) {
            //sum of digit
            sum += number % 10;
            number = parseInt(number / 10);
        }
        return sum;
    }
    //When given number digit sum  divisible remainder is zero
    is_harshad_no(number) {
        if (number % this.digit_sum(number) == 0) {
            //When Yes

            process.stdout.write(number + " Is an Harshad Number\n");
        } else {
            //When No

            process.stdout.write(number + " Is not an Harshad Number\n");
        }
    }
}

function main(args) {
    var obj = new MyNumber();
    //Test Case
    obj.is_harshad_no(72);
    obj.is_harshad_no(46);
    obj.is_harshad_no(135);
    obj.is_harshad_no(57);
}
main();

Output

72 Is an Harshad Number
46 Is not an Harshad Number
135 Is an Harshad Number
57 Is not an Harshad Number

Time Complexity

The time complexity of the given algorithm is primarily determined by the digit_sum function, which iterates through the digits of the number. Suppose the number has n digits. In the worst case, the loop in the digit_sum function will run n times to calculate the sum of the digits. Therefore, the time complexity of the algorithm is O(n).

Resultant Output Explanation

The given C program defines the digit_sum function to calculate the sum of digits and the is_harshad_number function to check if a number is a Harshad Number or not. The main function tests the algorithm with four test cases: 72, 46, 135, and 57.

  1. is_harshad_no(72);

    • The sum of digits for 72 is 7 + 2 = 9.
    • Check: 72 % 9 = 0
    • Output: 72 Is an Harshad Number
  2. is_harshad_no(46);

    • The sum of digits for 46 is 4 + 6 = 10.
    • Check: 46 % 10 = 6
    • Output: 46 Is not an Harshad Number
  3. is_harshad_no(135);

    • The sum of digits for 135 is 1 + 3 + 5 = 9.
    • Check: 135 % 9 = 0
    • Output: 135 Is an Harshad Number
  4. is_harshad_no(57);

    • The sum of digits for 57 is 5 + 7 = 12.
    • Check: 57 % 12 = 9
    • Output: 57 Is not an Harshad Number

The program correctly identifies the Harshad Numbers among the test cases based on the concept of divisibility by the sum of digits.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment