From 9c7c53844b19afc9cf9001010a94b2cf0f8ea793 Mon Sep 17 00:00:00 2001 From: Krish <68506145+KrishAgarwal2811@users.noreply.github.com> Date: Mon, 11 Oct 2021 16:29:22 +0530 Subject: [PATCH] Add check if a number is harshad Number or not (#2514) --- Maths/HarshadNumber.java | 60 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Maths/HarshadNumber.java diff --git a/Maths/HarshadNumber.java b/Maths/HarshadNumber.java new file mode 100644 index 000000000..22897cf38 --- /dev/null +++ b/Maths/HarshadNumber.java @@ -0,0 +1,60 @@ +// Wikipedia for Harshad Number : https://en.wikipedia.org/wiki/Harshad_number + +package Maths; + +import java.util.Scanner; + +public class HarshadNumber +{ + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.print("Enter a number : "); + long a = sc.nextLong(); + + checkHarshadNumber(a); + } + + /** + * A function to check if a number is Harshad number or not + * + * @param a The number which should be checked + */ + public static void checkHarshadNumber (long a) { + + long b = a; + int sum = 0; + + // this is just for showing the explanation else it's of no use you can ommit it + int[] each = new int[Long.toString(a).length()]; + + int c = 0; + + while (b > 0) { + sum += b % 10; + each[c] = (int)(b%10); + b /= 10; + c++; + } + + if (a % sum == 0){ + System.out.println(a + " is a Harshad Number"); + + // For you better explanation how is that a Harshad Number + System.out.println("\nExplaination :"); + + for (int i = each.length-1; i >=0; i--){ + System.out.print(each[i] + " "); + if (i != 0) { + System.out.print("+ "); + } + } + + System.out.println("= " + sum); + System.out.println(sum + " × " + (a / sum) + " = " + a); + } + + else { + System.out.println(a + " is not a Harshad Number"); + } + } +}