Home > Java > javaTutorial > How to check if a number is Harshad number in Java?

How to check if a number is Harshad number in Java?

王林
Release: 2023-09-24 21:01:08
forward
1183 people have browsed it

How to check if a number is Harshad number in Java?

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 of

Instance-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);
Copy after login

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();
Copy after login

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’;
Copy after login

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");
   }
}
Copy after login

Output

Given number: 21
21 is a Harshad number
Copy after login

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");      
   }
}
Copy after login

Output

Given number: 40
40 is a Harshad number
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template