Harshad Number in C Programming => Learning Duniya
What will be the programming for Harshad Number?
Today we are going to learn about the Harshad Number. We will learn definition , some example, programming and logic behind the Harshad Number and also look at the output either input number is Harshad Number or not.
What is Harshad number or Niven Number?
Harshad Number are those number whose number base is divisible by its sum of digit. Means a number is divisible by its same number base with its digit sum. It was first define by Indian Mathematician name Dattatreya Ramchandra Kaprekar. He named this number with a Sanskrit word which means joy-giver. It is also called as Niven Number this name was introduce by Ivan M. Niven in 1977.
Example :- Let us assume a number 12 then its digit sum is 1 + 2 = 3 and 12 divide by 3 is equal to 4 and all these number are of same base and also 12 is divisible by 3 so it is Harshad Number.
12 / (1+2) = 12 / 3 = 4.
Let us assume a number 14 , then its digit sum is 1 + 4 = 5 and 14 is not divisible be 5, all these number are of same base but 14 is not divisible by 5, so it is not Harshad Number.
Programming :-
#include<stdio.h>
#include<conio.h>
int main()
{
int num, newNumber = 0, temp , count; //Data type deceleration.
printf("Enter the number to check either it is Harshad Number or not");
scanf("%d",&num); //User input.
temp = num ;
while(temp > 0) //For splitting the number in it's digit.
{
count = temp % 10;
newNumber = newNumber + count;
temp = temp / 10;
}
if (num%newNumber == 0) //Condition for checking number is Harshad Number or not.
{
printf("%d is a Harshad Number ",num);
}
else
{
printf("%d is not a Harshad Number ",num);
}
getch();
return 0;
}
Output:-
Fig:-2
Logic:-
As definition of Harshad Number we come to know that a number is divisible by it digit sum then its only called Harshad Number. So for that we have use While loop to split the digit and add all the number. and after that we have use If else condition to check whether the number is Harshad Number or not by using modulus operator of C programming.
If you face any problem then comment in comment section otherwise visit my YouTube Channel and watch the video. All videos are in Hindi language.
It is very helpful and easy to understand , thank you 💐😍
ReplyDelete