Product program in java

Java Program to Find Product of Digits of a Number

In this program, we will learn to code the Java Program to Find Product of Digits of a Number. Let’s understand How to Find the Product of Digits of a Number in Java Programming Language. In previous programs, we have also discussed and learned to code the Java Program to add digits of a number. Using the same logic with some modification we can also code the Java Program to Find Product of Digits of a Number.

Product of Digits of a Number

Suppose, you are given the number 12345, so the program should calculate the result of 1 * 2 * 3 * 4 * 5 = 120 and 120 is the desired output.

Now, Let’s see the program to code the Java Program to Find Product of Digits of a Number.

Java Program to Find Product of Digits of a Number

import java.util.*; import java.lang.*; class Main < public static void main (String[] args) < Scanner sc = new Scanner(System.in); System.out.println("Enter the number: "); int num = sc.nextInt(); int n = num; // copy of the original number //Logic int product = 1; while(n>0) < int digit = n%10; product *= digit; n/=10; >System.out.println("Product of digits of the number "+num+" is "+product+" ."); > >
Enter the number: 12345 Product of digits of the number 12345 is 120 .

How Does This Program Work ?

Scanner sc = new Scanner(System.in); System.out.println("Enter the number: "); int num = sc.nextInt();

In this program, we take the number as input from the user using Scanner class in Java and store it in variable num of int datatype.

int n = num; // copy of the original number

Then, we preserve the original number and perform the calculation using the copy of the original number.

//Logic int product = 1; while(n>0)< int digit = n%10; product *= digit; n/=10; >

Then, we calculate the product of digits of the number using the while loop.

Suppose, the user enters a number 5634, then:

  • 1st While Loop Iteration: while (5634 != 0), the condition is True.
    • digit = n % 10 = 5634 % 10 = 4
    • product = product * digit = 1 * 4 = 4
    • n = n / 10 = 5634 / 10 = 563
    • 2nd While Loop Iteration: while (563 != 0), the condition is True.
      • digit = n % 10 = 563 % 10 = 3
      • product = product * digit = 4 * 3 = 12
      • n = n / 10 = 563 / 10 = 56
      • 3rd While Loop Iteration: while (56 != 0), this condition is also True.
        • digit = n % 10 = 56 % 10 = 6
        • product = product * digit = 12 * 6 = 72
        • n = n / 10 = 56 / 10 = 5
        • 4th While Loop Iteration: while (5 != 0), this condition is True.
          • digit = n % 10 = 5 % 10 = 5
          • product = product * digit = 72 * 5 = 360
          • n = n / 10 = 5 / 10 = 0
          • 5th While Loop Iteration: while (0 != 0), here the condition is False, so the while loop terminates and we get product = 360.
          System.out.println("Product of digits of the number "+num+" is "+product+" .");

          Then, we print the calculated area as the result using the System.out.println() function.

          This is the Java Program to Find Product of Digits of a Number.

          Conclusion

          I hope after going through this post, you understand the Java Program to Find Product of Digits of a Number. If you have any doubt regarding the topic, feel free to contact us in the comment section. We will be delighted to help you.

          • Java Program to Add Two Numbers
          • Java Program to Multiply Two Numbers
          • Java Program to Find ASCII Value of a Character
          • Java Program to Find Size of Different Data Types
          • Java Program to Find Quotient and Remainder

          Источник

          Saved searches

          Use saved searches to filter your results more quickly

          You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

          Typical Java exercise that shows how to build simple OO project and practice skills such as: how to choose appropriate data type for project, how to write clean and consistent code and etc.

          License

          MikhailMS/java-shop-example

          This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

          Name already in use

          A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

          Sign In Required

          Please sign in to use Codespaces.

          Launching GitHub Desktop

          If nothing happens, download GitHub Desktop and try again.

          Launching GitHub Desktop

          If nothing happens, download GitHub Desktop and try again.

          Launching Xcode

          If nothing happens, download Xcode and try again.

          Launching Visual Studio Code

          Your codespace will open once ready.

          There was a problem preparing your codespace, please try again.

          Latest commit

          Git stats

          Files

          Failed to load latest commit information.

          README.md

          Typical Java exercise that shows how to build simple OO project and practice skills such as: how to choose appropriate data type for project, how to write clean and consistent code and etc.

          This project will be written using Java 8 approach, rather than Java 7 and older. As Java 9 is released, maybe some of new Java features will be introduced if needed.

          Also I am using TDD approach to complete this project as I found this really convenient way to develop projects in Java. And as extra I’m trying to use Travis CI for building and testing project together with Codacy for keeping my code clean and consistent.

          • Erase dummy tests for Product, Basket, Order, Inventory, Shop classes and re-write them accordingly
            • ProductTest class
            • BasketTest class
            • OrderTest class
            • InventoryTest class
            • ShopTest class
            • Product class
            • Basket class
            • Order class
            • Inventory class
            • Shop class
            • Can save basket into DB
            • Can save order into DB
            • Can restore basket from DB
            • Can restore order from DB
            • Administrator user can
              • See all available products
              • See total cost of all products
              • Add products to inventory
              • Remove products from inventory
              • See orders completed by any user
              • See total cost of all completed orders
              • Add user to the system
              • Delete user from system
              • Filter products by name, price and weight
              • Filter orders by date
              • Filter orders by total price
              • See all available products
              • Add to basket
              • Remove from basket
              • Save basket
              • Restore basket
              • Save order
              • Restore order
              • Complete order
              • See all orders (client can see only his orders)
              • Filter products by name, price and weight
              • Filter orders by date
              • Filter orders by total price
              • Login screen
              • Can login into system and gain appropriate rights
              • Client GUI
                • See all available products
                • Add to basket
                • Remove from basket
                • Complete order
                • See all completed orders (client can see only his orders)
                • See total cost of all completed orders
                • Sort orders by total price
                • Filter orders by total price
                • Search for specific product by name
                • Sort products by name, price and weight
                • Filter products by name, price and weight
                • See all available products
                • See total cost of all products
                • Add new product to inventory
                • Add products to inventory
                • Remove products from inventory
                • See orders completed by any user
                • See total cost of all completed orders
                • Sort orders by date, total price
                • Filter orders by date/total price
                • Add user to the system
                • Delete user from system
                • Search for specific product by name
                • Sort products by name, price
                • Filter products by price, weight
                • Create main stage with TabPane(tab panel, that contains scenes)
                  • Main stage, which creates tab panels depending on the privilege level of the logged user
                  • Save basket
                  • Restore basket
                  • Login GUI
                  • Client GUI
                    • Highlighted parts
                    • Highlighted parts
                    • Review tests for the project so far
                    • MainStage GUI tests
                      • Client GUI tests
                      • Administrator GUI tests
                      • Once order is completed, service will update inventory in the DB and update local copy of the inventory
                      • It will also check, if order can be made, ie not enough products in the inventory
                      • If product(-s) not available to order, notify user and administrator
                      • When adding new product via form, product is added, but then I cannot change its amount
                      • METHOD DEPRECATED — WILL BE DELETED restoreBasketFromDB method shall be fixed, as currently it’s hardcoded to use fixed weight and price for all restored products in Basket
                      • When completing order as Client in GUI, basket is not reset. Should find a way to clear it
                      • When basket has been restored, restored products cannot be removed.

                      Improvements, that could be done later

                      • At the moment user can add more products into basket than there exist in inventory -> make them talk to one another
                      • At the moment there is no column to sort orders by date, but could be added. Will require extra column in GUI and extra filed in Order class
                      • At the moment data in rows does not update straight after product has been added/removed to/from basket/inventory, but rather after row been clicked, table been sorted or Details button been clicked
                      • At the moment, when new product added to basket/inventory, in order to display it, I delete all entries from observable list + its underlying list

                      Build and tested on MacOS and Java 1.8 (required)

                      About

                      Typical Java exercise that shows how to build simple OO project and practice skills such as: how to choose appropriate data type for project, how to write clean and consistent code and etc.

                      Источник

                      Java Program for Product of First N Numbers

                      Java Program for Multiplication of First N Numbers using for loop

                      This java program is used to compute the product of first n numbers using for loop.

                      Gets the limit value as user input and calculates the product value till limit value and prints the product value.

                      import java.util.Scanner; class ProductTest < public static void main(String[] values) < int product = 1, limit; Scanner sc = new Scanner(System.in); System.out.println("Calculates Product of first n numbers"); System.out.println("-------------------------------------"); System.out.print("Enter limit number:"); limit = sc.nextInt(); for(int i=1;i<=limit;i++) < product *= i; >System.out.println("Product of first '"+limit+"' numbers: " + product); > >
                      D:\Java_Programs>javac ProductTest.java D:\Java_Programs>java ProductTest Calculates Product of first n numbers ------------------------------------- Enter limit number:10 Product of first '10' numbers: 3628800

                      Java Program for Multiplication of First N Numbers using while loop

                      This java program is used to compute the product of first n numbers using while loop.

                      import java.util.Scanner; class ProductTest < public static void main(String[] values) < int product = 1, limit; Scanner sc = new Scanner(System.in); System.out.println("Calculates Product of first n numbers"); System.out.println("-------------------------------------"); System.out.print("Enter limit number:"); limit = sc.nextInt(); int i=1; while(i<=limit) < product *= i; i++; >System.out.println("Product of first '"+limit+"' numbers: " + product); > >
                      D:\Java_Programs>javac ProductTest.java D:\Java_Programs>java ProductTest Calculates Product of first n numbers ------------------------------------- Enter limit number:8 Product of first '8' numbers: 40320

                      Java Program for Multiplication of First N Numbers using do while loop

                      This java program is used to compute the product of first n numbers using do while loop.

                      import java.util.Scanner; class ProductTest < public static void main(String[] values) < int product = 1, limit; Scanner sc = new Scanner(System.in); System.out.println("Calculates Product of first n numbers"); System.out.println("-------------------------------------"); System.out.print("Enter limit number:"); limit = sc.nextInt(); int i=1; do < product *= i; i++; >while(i <=limit); System.out.println("Product of first '"+limit+"' numbers: " + product); >>
                      D:\Java_Programs>javac ProductTest.java D:\Java_Programs>java ProductTest Calculates Product of first n numbers ------------------------------------- Enter limit number:9 Product of first '9' numbers: 362880

                      Источник

                      Читайте также:  Java open with dialog
Оцените статью