C Program
#include <stdio.h> int main() { int n, sum=0, temp; scanf("%d", &n); temp = n; while(temp > 0) { sum += temp % 10; temp /= 10; } if(n % sum == 0) printf("%d is a Harshad Number", n); else printf("%d is not a Harshad Number", n); return 0; }
C Output
Input:
18Output:
18 is a Harshad Number
C++ Program
#include <iostream> using namespace std; int main() { int n, sum=0, temp; cin >> n; temp = n; while(temp > 0) { sum += temp % 10; temp /= 10; } if(n % sum == 0) cout << n << " is a Harshad Number"; else cout << n << " is not a Harshad Number"; return 0; }
C++ Output
Input:
21Output:
21 is not a Harshad Number
JAVA Program
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), sum = 0, temp = n; while(temp > 0) { sum += temp % 10; temp /= 10; } if(n % sum == 0) System.out.println(n + " is a Harshad Number"); else System.out.println(n + " is not a Harshad Number"); } }
JAVA Output
Input:
12Output:
12 is a Harshad Number
Python Program
n = int(input()) s = sum(int(d) for d in str(n)) if n % s == 0: print(n, "is a Harshad Number") else: print(n, "is not a Harshad Number")
Python Output
Input:
25Output:
25 is not a Harshad Number
In-Depth Learning – Entire Concept in Paragraphs
Social Plugin