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:

PhD researcher at Friedrich-Schiller University Jena, Germany. I'm a physicist specializing in computational material science. I write efficient codes for simulating light-matter interactions at atomic scales. I like to develop Physics, DFT and Machine Learning related apps and softwares from time to time. Can code in most of the popular languages. Like to share my knowledge in Physics and applications using this Blog and a YouTube channel.
[wpedon id="7041" align="center"]

Leave a Reply

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