Problem: Write a C program to check whether a number is a Harshad Number or not.

Harshad number (or Niven) is an integer that is divisible by the sum of its digits.

Example: – 21, 111, 153.

Steps to check Harshad Number in C:

  1. Take a number as input.
  2. Sum the digits of the number.
  3. Find the remainder of number/sum i.e number%sum
  4. If the remainder is 0 then the input is Harshad number.
  5. Else Not.

Here is the implementation of the steps in C.

#include<stdio.h>
#include<conio.h>
 
int main()
{
    int number,original_number,sum=0;
    int digit;
 
    printf("Enter a number \n");
    scanf("%d",&original_number);
 
    number=original_number;//copying the original number
 
    while(number!=0)
    {
        digit=number%10;
        sum+=digit;
        number=number/10;
    }
 
    if(original_number%sum==0)
        printf("HARSHAD NUMBER \n");
    else
        printf("NOT A HARSHAD NUMBER");
 
    return 0;
}

Output

Enter a number
153
HARSHAD NUMBER

In this tutorial, we learned to check Harshad Number in C.

Leave a Reply

four × three =