HARSHAD Number or Not in C Program
ADVERTISEMENTS
Harshad number or not in c program.
In this article, you will learn how to check whether an input number is a Harshad number.
Example
INPUT A NUMBER: 156
156 IS A HARSHAD NUMBER
What are the Harshad Numbers?
- Input Number is
54
- Definition
54
/(5 + 4)
, then Reminder == 0 - It means 54 is a Harshad Number
- Second example:
- Input is
25
25
/(2 + 5)
, then Reminder != 0- It means 25 is not a Harshad Number
There are 4 ways to solve this problem:
- Using While loop
- Using Do-while loop
- Using For loop
- Using Function
You should know about the following topics in C programming to understand this program:
- If-else statement
- While loop in C
- Do-while loop in C
- For loop in C
- C Functions
1 - HARSHAD Number or Not in C using While loop
// HARSHAD Number or Not in C using While loop
#include <stdio.h>
// It's the main function of the program
int main()
{
int num, sum = 0;
// Step-1 Take an input from the User
printf("INPUT A NUMBER: ");
scanf("%d", &num);
// Step-2 Store the input value into another variable
int original = num;
// Step-3 Use a loop to check the Harshad number
while (original != 0)
{
sum += original % 10;
original /= 10;
}
// Step-4 Compare the input value with the sum of divisors of the input value
if (num % sum == 0)
printf("\n%d IS A HARSHAD NUMBER\n", num);
else
printf("\n%d IS NOT A HARSHAD NUMBER\n", num);
return 0;
}
Output
INPUT A NUMBER: 156
156 IS A HARSHAD NUMBER
2 - HARSHAD Number or Not in C using Do-while loop
// HARSHAD Number or Not in C using Do-while loop
#include <stdio.h>
// It's the main function of the program
int main()
{
int num, sum = 0;
// Step-1 Take an input from the User
printf("INPUT A NUMBER: ");
scanf("%d", &num);
// Step-2 Store the input value into another variable
int original = num;
// Step-3 Use a loop to check the Harshad number
do
{
sum += original % 10;
original /= 10;
} while (original != 0);
// Step-4 Compare the input value with the sum of divisors of the input value
if (num % sum == 0)
printf("\n%d IS A HARSHAD NUMBER\n", num);
else
printf("\n%d IS NOT A HARSHAD NUMBER\n", num);
return 0;
}
3 - HARSHAD Number or Not in C using For loop
// HARSHAD Number or Not in C using For loop
#include <stdio.h>
// It's the main function of the program
int main()
{
int num, sum = 0;
// Step-1 Take an input from the User
printf("INPUT A NUMBER: ");
scanf("%d", &num);
// Step-2 Store the input value into another variable
int original = num;
// Step-3 Use a loop to check the Harshad number
for (original != 0; original /= 10;)
sum += original % 10;
// Step-4 Compare the input value with the sum of divisors of the input value
if (num % sum == 0)
printf("\n%d IS A HARSHAD NUMBER\n", num);
else
printf("\n%d IS NOT A HARSHAD NUMBER\n", num);
return 0;
}
Output
INPUT A NUMBER: 125
125 IS NOT A HARSHAD NUMBER
4 - HARSHAD Number or Not in C using Function
// HARSHAD Number or Not in C using Function
#include <stdio.h>
// Function to check the Harshad number
int is_harshad_number(int number)
{
int sum = 0;
int original = number; // Store the input value in another variable
while (original != 0)
{
sum += original % 10;
original /= 10;
}
// If the condition is true It will return '1' otherwise return '0'
return number % sum == 0;
}
// It's the main function of the program
int main()
{
int num;
// Step-1 Take an input from the User
printf("INPUT A NUMBER: ");
scanf("%d", &num);
// Step-2 Call the is_harshad_number(); function to check the Harshad number
if (is_harshad_number(num))
printf("\n%d IS A HARSHAD NUMBER\n", num);
else
printf("\n%d IS NOT A HARSHAD NUMBER\n", num);
return 0;
}
Output
INPUT A NUMBER: 54
54 IS A HARSHAD NUMBER