TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program to determine whether a given number is a Harshad number

Program to determine whether a given number is a Harshad number on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

<< Back to PROGRAM

Program to determine whether a given number is a Harshad number

Explanation

In this program, we need to determine whether given is Harshad or not.

Harshad number

A number is said to be the Harshad number if it is divisible by the sum of its digit.

For example, if number is 156, then sum of its digit will be 1 + 5 + 6 = 12. Since 156 is divisible by 12. So, 156 is a Harshad number.

Some of the other examples of Harshad number are 8, 54, 120, etc.

To find whether the given number is a Harshad number or not, calculate the sum of the digit of the number then, check whether the given number is divisible by the sum of its digit. If yes, then given number is a Harshad number.

Algorithm

  1. Define and initialize variable num.
  2. Make a copy of the num by storing the value of num in variable n.
  3. If num is greater than 0, then calculate the remainder by dividing num with 10.
  4. Add rem to a variable sum.
  5. Divide num by 10.
  6. Repeat the steps from 3 to 5, to calculate the sum of all digits present in a given number.
  7. If n (copy of original number) is divisible by the sum, then given number is a Harshad number. Else, given number is not a Harshad number.

Solution

Python

num = 156;
rem = sum = 0;
 
#Make a copy of num and store it in variable n
n = num;
 
#Calculates sum of digits
while(num > 0):
    rem = num%10;
    sum = sum + rem;
    num = num//10;
 
#Checks whether the number is divisible by the sum of digits
if(n%sum == 0):
    print(str(n) + " is a harshad number");
else:
    print(str(n) + " is not a harshad number");

Output:

 156 is a harshad number

C

#include <stdio.h>
 
int main()
{
    int num = 156;
    int rem = 0, sum = 0, n;
    
    //Make a copy of num and store it in variable n
    n = num;
    
    //Calculates sum of digits
    while(num > 0){
        rem = num%10;
        sum = sum + rem;
        num = num/10;
    }
    
    //Checks whether number is divisible by sum of digits
    if(n%sum == 0)
        printf("%d is a harshad number", n);
    else
        printf("%d is not a harshad number", n);
 
    return 0;
}

Output:

156 is a harshad number

JAVA

public class HarshadNumber
{
    public static void main(String[] args) {
        int num = 156;
        int rem = 0, sum = 0, n;
        
        //Make a copy of num and store it in variable n
        n = num;
        
        //Calculates sum of digits
        while(num > 0){
            rem = num%10;
            sum = sum + rem;
            num = num/10;
        }
        
        //Checks whether number is divisible by sum of digits
        if(n%sum == 0)
            System.out.println(n + " is a harshad number");
        else
            System.out.println(n + " is not a harshad number");
    }
}

Output:

156 is a harshad number

C#

using System;
                    
public class HarshadNumber
{
    public static void Main()
    {
        int num = 156;
        int rem = 0, sum = 0, n;
        
        //Make a copy of num and store it in variable n
        n = num;
        
        //Calculates sum of digits
        while(num > 0){
            rem = num%10;
            sum = sum + rem;
            num = num/10;
        }
        
        //Checks whether number is divisible by sum of digits
        if(n%sum == 0)
            Console.WriteLine(n + " is a harshad number");
        else
            Console.WriteLine(n + " is not a harshad number");
    }
}

Output:

156 is a harshad number

PHP

<!DOCTYPE html> 
<html> 
<body> 
<?php 
$num = 156;
$rem = $sum = 0;
 
//Make a copy of num and store it in variable n
$n = $num;
 
//Calculates sum of digits
while($num > 0){
    $rem = $num%10;
    $sum = $sum + $rem;
    $num = intval($num/10);
}
 
//Checks whether number is divisible by sum of digits
if($n%$sum == 0)
    print($n . " is a harshad number");
else
    print($n . " is not a harshad number");  
?> 
</body> 
</html> 

Output:

156 is a harshad number

Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf