Find product of digits of a number in java | codeYourslf

In this article you can learn how to write a program to find out the product of digits of a number in java by writing code in mobile phone.
4 min read

Product of digits of a number in java

Find product of digits of a number in java | codeYourslf

In this article you can learn how to write a program to find out the product of digits of a number in java by writing code in mobile phone.

Yes I always use my android phone (java N-IDE app) to write code for all programs.

Approach:-1 "find product of digits of a number in java using while loop"

public class usingWhileLoop {

    public static void main(String[] args){

      int num = 2316;

      int product = 1;

      

      while(num != 0)

      {

        int lastDigit = num % 10;

        product = product * lastDigit;

        num = num / 10;

      }

      System.out.println("The product is: "+product);

    }

}      
+
Explanation

Here our job is to find the digits one by one and multiply it to a product variable individually. 

By using modulus operator(%), first I am fetching the last digit of the number and multiplying it to product variable (which is initially created).                          

Now we have to remove the last digit, we can do by simply dividing the number with 10

This loop will go upto the number > 0 or number not equal to 0 (num != 0)

After multiplying first digits, the loop will terminate and finally the value of product of digits will be stored in the product variable.

Output

find product of digits of a number in java using while loop

Also check out:-

Friends if you learn something new from the article, please share with your friends.

Hey if you have any questions or any doubt please write it down on the comment below, I will definitely answer to your questions.

`

You may like these posts

Post a Comment

Hey Please 👏, feel free to share your opinion 🌍