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 theoretical, computational and experimental condensed matter physics. I like to develop Physics 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.



Leave a Reply

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