C Online Compiler
Example: HARSHAD Number or Not in C using Function
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// 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; }
54
Output
Clear
ADVERTISEMENTS