// Wikipedia for Harshad Number : https://en.wikipedia.org/wiki/Harshad_numberpackage 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 itint[] 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 NumberSystem.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");}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。