How to check whether a given number is a harshad number in Node.js

1 Answer

0 votes
const n = 171;  
let sum = 0;
let temp = n;  
         
while (temp > 0) {  
    let reminder = temp % 10;  
    sum = sum + reminder;  
    temp = Math.trunc(temp / 10);  
}  
       
// 1 + 7 + 1 = 9 : 171 % 9 = 0 <- harshad   
if (n % sum == 0)  
    console.log(n + " is a harshad number");  
else
    console.log(n + " is not a harshad number");  
      
   
   
   
   
/*
run:

171 is a harshad number
    
*/

 



answered Nov 16, 2022 by avibootz

Related questions

...