Q:

Write a program in C to check if a number is Harshad Number or not

0

Write a program in C to check if a number is Harshad Number or not


Expected Output :
Input a number: 9
The given number is a Harshad Number.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

# include <stdio.h>
# include <stdlib.h>
# include <stdbool.h>


bool chkHarshad(int n)
{
    int s = 0;
	int tmp;
    for (tmp=n; tmp>0; tmp /= 10)
        s += tmp % 10;
    return (n%s == 0);
}
int main()
{
    int hdno;
	printf("\n\n Check whether a number is Harshad Number or not: \n");
	printf(" ---------------------------------------------------\n");
	printf(" Input a number: ");
	scanf("%d",&hdno);
 
     if( chkHarshad(hdno))
	printf(" The given number is a Harshad Number.\n");
    else
	printf(" The given number is not a Harshad Number.\n");
    return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now