What is Harshad or niven number?
A whole number in base ten that is severable by the total of its digits is alleged to be a Harshad or Niven variety. associate n-Harshad or n niven variety is associate whole number severable by the total of its digit in base n. it is called as Harshad and niven number
Example of Harshad or niven number
Input: 3768
Output: 3768 is a Harshad or niven Number
Input: 18
Output: 18 is a Harshad or niven Number
Input: 202
Output: 202 is not a Harshad or niven Number
How it’s work Harshad or niven number?
- Take any number(6804) and calculate the sum of digit the number (6+8+0+4 = 18) and divide 6804 by 18 then the outcome is 378.
- After coming outcome takes as input form again(378) and calculate the sum of digit again of that number(3+7+8 =18) and divide 378 by 18 the second outcome is 21.
- After coming second outcome takes as input form again(21) and calculates the sum of the digit again of that number(2+1 =3) and divides 21 by 3 the third outcome is 7.
- After coming third outcome takes as input form(7)and calculates the sum of digit again of that number is(7) and divide that 7 divides by 7 and finally come 1.
- After coming one Harshad number or niven is proved.
Algorithms of harshad number
· STEP 1: START.
· STEP 2: intialize digit = any number
· Step 3: intialize rem =0, sum =0.
· STEP 4: create variable n.
· STEP 5: n = digit.
· STEP 6: REPEAT STEP 6 to STEP 8 UNTIL (digit>0)
· STEP 7: rem =digit%10.
· STEP 8: sum = sum + rem.
· STEP 9: digit = digit/10
Let’s start with the program in python
- Create a digit variable and take any number in the form of input
digit = int(input(“enter the number”))
- Initialize remainder and sum is zero remainder variable name is rem sum variable name is a sum
rem = 0
sum = 0
- Create a variable and assign the digit variables
n = digit
- I run while loop till digit is greater than 0 and then apply the logic of Harshad number
while(digit > 0):
rem = digit%10;
sum = sum + rem;
digit = digit//10;
- After applying the logic I run if else statement to check and print the harshad number
if(n%sum == 0):
print(str(n) + " is a harshad or niven number");
else:
print(str(n) + " is not a harshad or niven number");
The final output is :
Enter the digit -- 3768
3768 is a Harshad or niven number
Enter the digit -- 202
202 is not a Harshad or niven number
#somaymangla #somaymangla #somaymangla
#somaymangla
#somaymangla
Author
Thanks for this amazing information
ReplyDelete