Harshad number or not
Write a C Program to find number is Harshad number or not.
In this program we will discuss of number is harshad number or not in C programming. In mathematics, a Harshad number is a number that is divisible by the sum of its digits. We use while loop statement with following condition. Input consists of 1 integer.
Ex– Number is 21
it is divisible by own sum (1+2) of its digit(2,1)
So it is harshad number
Some other harshad number are 156,54,120 etc.
Step 1- Enter the number to be check.
Step 2- store the number in temporary variable.
Step 3- Initialise the while loop until the temp is not equal to zero
(i) Calculate the remainder of the temp,divided with the 10 and store in digit
(ii) add the digit with sum and store it in the sum.
(ii) divide the temp with the 10 and store in the temp;
Step 4- find modulus of the number with sum and store in the res;
Step 5- if res equal to zero than given number is harshad number else the given number is not a harshad number.
Step 6- Stop.
//To initialize of variable
int number,temp,sum = 0, digit, res;
//To take user input
printf("enter any number : ");
//store in temporary variable
temp = number;
//use while loop with this condition
//to find last digit
digit=temp % 10;
sum = sum + digit;
temp = temp / 10;
res = number % sum;
//check result is equal is to 0
if(res == 0)
printf("%d is Harshad Number",number);
printf("%d is not Harshad Number",number);
enter any number : 21
21 is Harshad Number
enter any number : 15
15 is not Harshad Number