C Program to print Harshad Numbers in a given range

programming coding blue hands digital keyboard fingers
Harshad(or Niven) Numbers are those integers who are who are divisible by the sum of their digits.

The following program prints the Harshad Numbers within a range specified by the user.

PROGRAM:

/********************************************
 **********HARSHAD NUMBERS*******************
 (c) 2017 Manas Sharma - https://bragitoff.com
 *******************************************/
#include<stdio.h>
#include<stdlib.h>
main(){
  int i,j,init,final,n,sum;
  printf("Enter the starting point(a):\n");
  scanf("%d",&init);
  printf("Enter the ending point(b):\n");
  scanf("%d",&final);
  printf("\nThe Harshad Numbers are as follows:\n");
  for(i=init;i<=final;i++){
    n=i;
    sum=0;
    while(n>0){
      sum=sum+n%10;
      n=n/10;
    }
    if(i%sum==0){
      printf("%d\n",i);
    }
  }
}

OUTPUT:

[wpedon id="7041" align="center"]

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.