Program to determine whether a number is Harshad (Niven) number in Python. The source code contains if else conditional statement, while loop and input command. This program is for Beginners ,Class 11,12 and B Tech Computer Science students.
Program to determine whether a number is Harshad (Niven) number in Python
What is Harshad 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.
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.
Source code :
num = int(input("Enter the no to be checked for Harshad number") )
rem = sum = 0;
n = num;
while(num > 0):
rem = num%10;
sum = sum + rem;
num = num//10;
if(n%sum == 0):
print(str(n) + " is a harshad number");
else:
print(str(n) + " is not a harshad number");
Source code Explanation :
Step 1: Enter a number
num = int(input(“Enter the no to be checked for Harshad number”))
rem = sum = 0;
Step 2:Make a copy of num and store it in variable n
n = num;
Step 3:Calculates sum of digits
while(num > 0):
rem = num%10;
sum = sum + rem;
num = num//10;
Step 4: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 :
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> RESTART: C:/csstudy/download source code files/Program to print Happy Numbers between 1 and 100 in Python.py Enter the number to be checked 52 52 is not a harshad number >>> RESTART: C:/csstudy/download source code files/Program to print Happy Numbers between 1 and 100 in Python.py Enter the number to be checked 156 156 is a harshad number >>>
Output Explanation:
Step1 : Enter the number to be checked 156
Step2 : print 156 is a Harshad number
Conclusion:
The program is running fine.
Thank you for reading, learning and downloading source code of Program to determine whether a number is Harshad (Niven) number in Python. The source code contains if else conditional statement, while loop and input command. This program is for beginners , class 11,12 and B Tech computer science students.
For more programs click below on the links:
Python Programs Archives – CS Study
Computer Science with Python Practical Book Solutions Class 11 Term -2 – CS Study
Python Projects for Class 12 with Source Code (Mysql Connectivity) – CS Study