Python program to determine whether the given number is a Harshad Number.

python tutorials and learn python

Created with Sketch.

Python program to determine whether the given number is a Harshad Number.

If a number is divisible by the sum of its digits, then it will be known as a Harshad Number.

For example:

The number 156 is divisible by the sum (12) of its digits (1, 5, 6 ).

Some Harshad numbers are 8, 54, 120, etc.

In this program, integer value is predefined, we don?t need to put integer value to determine whether the given number is a Harshad number or not by following the algorithm as given below:

ALGORITHM:

  • STEP 1: START
  • STEP 2: SET num = 156, rem =0, sum =0
  • STEP 3: DEFINE n
  • STEP 4: n = num
  • STEP 5: REPEAT STEP 6 to STEP 8 UNTIL (num>0)
  • STEP 6: rem =num%10
  • STEP 7: sum = sum + rem
  • STEP 8: num = num/10
  • STEP 9: if(n%sum==0) then PRINT “Yes” else PRINT “No”
  • STEP 10: END

PROGRAM:

  1. num = 156;
  2. rem = sum = 0;
  3. #Make a copy of num and store it in variable n  
  4. n = num;
  5. #Calculates sum of digits  
  6. while(num > 0):
  7.     rem = num%10;
  8.     sum = sum + rem;
  9.     num = num//10;
  10. #Checks whether the number is divisible by the sum of digits  
  11. if(n%sum == 0):
  12.     print(str(n) + ” is a harshad number”);
  13. else:
  14.     print(str(n) + ” is not a harshad number”);

Output:

156 is a harshad number

Leave a Reply

Your email address will not be published. Required fields are marked *