Harshad number is divisible by the sum of its digits. Simply put, if the sum of all the digits of a number is a factor of that number, then it is a Harshad number.
In this article, we will see how to check Harshad number using Java programming language.
Show you some examples
Example 1
Enter the number as 18
Let us check it using the logic of Harshad number −
The sum of the digits of the number = 1 8 = 9.
So, 18 is divisible by 9.
Therefore, 18 is a Harshad number.
Instance-2
is translated as:Instance-2
The number entered is 3
Let us check it using the logic of Harshad number −
The sum of the digits of the number = 3.
So, 3 can be divided by 3.
Therefore, 3 is the Harshad number.
The Chinese translation ofInstance-3
is:Instance-3
Enter the number as 15
Let us check it using the logic of Harshad number −
The sum of the digits of the number = 1 5 = 6.
So, 15 is not divisible by 6.
Therefore, 14 is not a Harshad number.
Some other examples of Harshad numbers include 1, 2, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, etc.
grammar
Convert an integer value to a string value using the built-in toString() method.
The following is the syntax to convert an integer value to a string value, then find its length and assign the length to an integer variable to get the total number of digits in the number -
String str = Integer.toString(input_number);
To get the length of an integer, we will use the built-in length() method of the Java String class Method returns the length of the String object.
int length = st.length();
To get the character at a specific position/index in a string, we use the charAt() method. Where charAt(i)-‘0’ returns the actual integer value.
int num = st.charAt(i)-‘0’;
Where "st" refers to the string, "i" is the iterator variable that iterates over the string.
algorithm
Algorithm 1
Step 1 - Get an integer via initialization or user input.
Step 2 - Find the sum of each digit of the number by iterating through each digit of the number.
Step 3 - Then check if the original number is divisible by the sum of all digits of the number. The given number is a Harshad number if it is divisible, otherwise it is not a Harshad number
Algorithm 2
Step 1 - Get an integer via initialization or user input.
Step 2 - Convert the integer to a string using the built-in toString() method.
Step 3 - Find the length of the string by using the built-in length() method.
Step 4 - Then by using a for loop, iterate until the length of the string and get an integer value 0' from the string using charAt(i)-' And record the sum of all numbers.
Step 5 - Then check if the original number is divisible by the sum of all digits of that number. The given number is a Harshad number if it is divisible, otherwise it is not a Harshad number.
Multiple methods
We provide solutions in different ways
Do not use strings
By using strings
Let’s look at the program and its output one by one.
Method 1: Not using strings
In this method, an integer value will be initialized in the program, and then by using Algorithm-1 we can check whether a number is a Harshad number.
Example
public class Main{ //main method public static void main(String args[]){ //Declared an integer variable and initialized a number as value int originalNumber = 21; //printing the given number to be checked System.out.println("Given number: "+originalNumber); //keep a copy of original number int copyOfOriginalNumber = originalNumber; //initialize sum as 0 int sum = 0; //Find sum of all digits of the number //continue the loop till the number is greater than 0 while(originalNumber > 0){ //get the rightmost digit of the number by using % operator int rem = originalNumber%10; //add the digit(rem) to sum sum = sum + rem; //remove the rightmost digit from number and get the updated number originalNumber = originalNumber/10; } //printing the result if(copyOfOriginalNumber % sum == 0) System.out.println(copyOfOriginalNumber+" is a Harshadnumber"); else System.out.println(copyOfOriginalNumber+" is not a Harshadnumber"); } }
Output
Given number: 21 21 is a Harshad number
Method 2: Using strings
In this method, an integer value will be initialized in the program and then using Algorithm-2 we will check whether the number is a Harshad number or not.
Example
public class Main{ //main method public static void main(String args[]){ //Declared an integer variable and initialized a number as value int originalNumber = 40; //printing the given number to be checked System.out.println("Given number: "+originalNumber); //keep a copy of original number int copyOfOriginalNumber = originalNumber; //initialize sum as 0 int sum = 0; //convert the integer to string by using toString() method String str = Integer.toString(originalNumber); //find length of String by using length() method //which is nothing but total number of digits in the given number int length=str.length(); //iterate the String and get the digits by using charAt(i)-'0' //find the sum of digits for(int i = 0; i < length; i++){ sum += str.charAt(i)-'0'; } //printing the result if(copyOfOriginalNumber % sum == 0) System.out.println(copyOfOriginalNumber+" is a Harshad number"); else System.out.println(copyOfOriginalNumber+" is not a Harshad number"); } }
Output
Given number: 40 40 is a Harshad number
In this article, we explored how to check if a number is a Harshad number in Java using different methods.
The above is the detailed content of How to check if a number is Harshad number in Java?. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Zend Studio 13.0.1
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
