Q.10 WAP in C/ C++/ java to check whether a number is a Harshad Number or not.
In recreational mathematics, a Harshad number in a given number base is an integer that is divisible by the sum of its digits when written in that base.
Example: Number 200 is a Harshad Number because the sum of digits 2 and 0 and 0 is 2(2+0+0) and 200 is divisible by 2. Number 171 is a Harshad Number because the sum of digits 1 and 7 and 1 is 9(1+7+1) and 171 is divisible by 9.
Solution :
#include<stdio.h>
int sumofdigits(int x) // function to compute sum of digits
{
int sum=0;
while(x>0)
{
sum+=x%10;
x=x/10;
}
return sum;
}
void main()
{
int n, sod;
printf("\nEnter any positive number : ");
scanf("%d", &n);
if (n<0)
{
printf("\nInvalid input! Enter the value again -->");
main(); // calls the main() function again
}
else
{
sod=sumofdigits(n);
if(n%sod == 0)
printf("%d is a Harshad Number\n",n);
else
printf("%d Not a harshad number\n",n);
}
} // end of main()
Wow ho!...Where haveu got these numbers...Its truly amazing...And I even didnot know that a number name "Harshad"(from Harshad Mehta) was there for so many years ....Thanks a ton buddy 👍
ReplyDelete