Niven Number solution using c language--programming_info problem 23

Niven Number or Harshad Number:


  1. #include <stdio.h>    
  2.      
  3. int main()    
  4. {    
  5.     int num = 156;    
  6.     int rem = 0, sum = 0, n;    
  7.         
  8.     //Make a copy of num and store it in variable n    
  9.     n = num;    
  10.         
  11.     //Calculates sum of digits    
  12.     while(num > 0){    
  13.         rem = num%10;    
  14.         sum = sum + rem;    
  15.         num = num/10;    
  16.     }    
  17.         
  18.     //Checks whether number is divisible by sum of digits    
  19.     if(n%sum == 0)    
  20.         printf("%d is a harshad number", n);    
  21.     else    
  22.         printf("%d is not a harshad number", n);    
  23.      
  24.     return 0;    
  25. }    


Output:

156 is a harshad number
Share:

No comments:

Post a Comment

Translate

Recommended platforms

  1. codechef
  2. hackerrank
  3. codeforces
  4. leetcode
  5. hackerearth

Popular Posts

programming_info. Powered by Blogger.

Blog Archive

Recent Posts

other platforms

  • geeks for geeks
  • w3schools
  • codepen
  • skillshare
  • udemy

Pages

reader support Support