Q. Write an algorithm and program to check harshad (or niven) number.

Program to check Harshad (Or Niven) Number : This section focuses on Harshad Number algorithm and program. This program should be practiced to improve the coding skills required for various interviews (campus interviews, walk-in interviews), coding rounds etc.

Harshad (Or Niven) Number : A harshad number (or Niven number) in a given number base, is an integer that is divisible by the sum of its digits when written in that base. Harshad numbers in base n are also known as n-harshad (or n-Niven) numbers. Here we are given a number n and our task is to check if the number is divisible by sum of digits or not.

For Example :-

Input : n = 18
Output : Yes, The number is Harshad Number
Explanation : As we can see that input number is 18. The sum of digit is 1 + 8 = 9. As, 18 is divisible by 9. So, output for the following code is "Yes, The number is Harshad Number".

Now let us understand what logic we need to use in this :
1. Firstly, we need to extract all the digits from the number using % operator.
2. Calculate the sum of each digit.
3. Check if the number is divisible by sum or not.

Algorithm To Check Harshad Number

\\Algorithm To Check Harshad Number
START
Step 1 : Initialize sum as 0. 
Step 2 : Loop begin from n to 0.
            Calculate -> sum += temp % 10.
Step 3 : Divide the original number by sum.
Step 4 : Check if divisible by sum or not.
Stop


Program To Check Harshad Number

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • //C Program To Check Harshad Number
    #include <stdio.h> 
      
    int main()
    {
        int num = 12, sum = 0; 
        //for loop to calculate sum
        for (int temp = num; temp > 0; temp /= 10) 
            sum += temp % 10;
        //Check if the number is divisible by sum or not
        if(num%sum == 0)
        {
            printf("Yes, The number is Harshad Number");
        }
        else
        {
            printf("No, The number is not Harshad Number");
        }
    
        return 0;
    }
    
    //C++ Program To Check Harshad Number
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int num = 12, sum = 0; 
        //for loop to calculate sum
        for (int temp = num; temp > 0; temp /= 10) 
            sum += temp % 10;
        //Check if the number is divisible by sum or not
        if(num%sum == 0)
        {
            cout<<"Yes, The number is Harshad Number";
        }
        else
        {
            cout<<"No, The number is not Harshad Number";
        }
        return 0;
    }
    
    //Java Program To Check Harshad Number
    
    public class LFC
    {
        public static void main(String[] args) {
            int num = 12, sum = 0; 
            //for loop to calculate sum
            for (int temp = num; temp > 0; temp /= 10) 
                sum += temp % 10;
            //Check if the number is divisible by sum or not
            if(num%sum == 0)
            {
                System.out.println("Yes, The number is Harshad Number");
            }
            else
            {
                System.out.println("No, The number is not Harshad Number");
            }
            
        }
    }
    
    #Python Program To Check Harshad Number
    
    num = 12
    sum = 0
    temp = num
    #while loop to calculate sum
    while temp > 0 : 
        sum = sum + temp % 10
        temp = temp // 10
    
    #Check if the number is divisible by sum or not
    if num%sum == 0:
        print("Yes, The number is Harshad Number")
    else:
        print("No, The number is not Harshad Number")
    
    //C# Program To Check Harshad Number
    
    using System;
    class LFC {
      static void Main() {
            int num = 12, sum = 0; 
            //for loop to calculate sum
            for (int temp = num; temp > 0; temp /= 10) 
                sum += temp % 10;
    
            //Check if the number is divisible by sum or not
            if(num%sum == 0)
            {
                Console.WriteLine("Yes, The number is Harshad Number");
            }
            else
            {
                Console.WriteLine("No, The number is not Harshad Number");
            }
      }
    }
    
    <?php
    //PHP Program To Check Harshad Number
    
    $num = 12;
    $sum = 0;
    $temp = $num;
    //for loop to calculate sum
    for ($temp = $num; $temp > 0; 
                         $temp /= 10) 
            $sum += $temp % 10; 
    
    //Check if the number is divisible by sum or not
    if($num%$sum == 0)
        echo "Yes, The number is Harshad Number";
    else
        echo"No, The number is not Harshad Number"; 
    
    //C Program To Check Harshad Number
    #include <stdio.h> 
      
    int main()
    {
        int num = 12, sum = 0; 
        //for loop to calculate sum
        for (int temp = num; temp > 0; temp /= 10) 
            sum += temp % 10;
    
        //Check if the number is divisible by sum or not
        if(num%sum == 0)
        {
            printf("Yes, The number is Harshad Number");
        }
        else
        {
            printf("No, The number is not Harshad Number");
        }
    
        return 0;
    }
    

    Output

    Yes, The number is Harshad Number
    

    Recommended Programs

       Program to check whether the given number is palindrome or not.
       Program to check whether the given string is pangram or not.