Elen Mouradian
Elen Mouradian

Reputation: 665

Multiplying digits of a number until you reach a one digit number

Assume, you input the number 546, then you should find the product of its digits, which is 546=120, then multiply the digits of 120 until and so on, continue until you get a one digit number. Here's the code I wrote, but the loop doesn't work correctly and I've tried to fix it, but nothing changed. Could you please help me?

import java.util.*;
public class Product {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int a = keyboard.nextInt();
        System.out.println("Please input the number");
        numberOfProducts(a);


    }
    public static void numberOfProducts(int n){
        int product = 1;
        while(n>10) {
            int current = n;
            while (current != 0) {
                product = product * (current % 10);
                current = current / 10;
                n = product;
                System.out.println(product);
            }
        }

    }

}

Upvotes: 0

Views: 4096

Answers (6)

Sashikiran
Sashikiran

Reputation: 1

    function getProductUntilSingleDigit(n) {
  let product = 1;
  while (n > 0 || product > 9) {
    if (n == 0) {
      n = product;
      product = 1;
    }
    product = product * (n % 10);
    n = Math.floor(n / 10);
  }
  return product;
}

console.log(getProductUntilSingleDigit(546));

Upvotes: 0

PeterKrcmar
PeterKrcmar

Reputation: 11

Actually your code is very close to being correct, the only thing you're doing wrong is that you are not resetting the product variable between iterations. You simply need to move the int product = 1; instruction inside the outer while loop.

Also, if you want a single digit at the end, your condition should be while(n >= 10) or while(n > 9), since 10 is still 2 digits, so we need one more iteration !

Final remark: sometimes it's easier to break your code into smaller pieces. It is easier to understand and easier to test/debug ! For example you could have first created a function productOfDigits(n) that returns the result of a single iteration, and then another function productOfDigitsUntilSingleDigit(n) that repeatedly calls the previous function.

Upvotes: 1

Jaya
Jaya

Reputation: 1

import java.util.*;
public class Product {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int a = keyboard.nextInt();
        System.out.println("Please input the number");
        numberOfProducts(a);
    }
      public static void numberOfProducts(int n){
        int product = 1; 
  
        while (n != 0)   { 
        product = product * (n % 10); 
        n = n / 10; 
        } 
    
        if(product > 10) {
            System.out.println("Intermediate result="+product);
            numberOfProducts(product);
        }
        else
            System.out.println("Final 1 digit product="+product);

    }
}

Upvotes: 0

Stijn Dejongh
Stijn Dejongh

Reputation: 167

After looking through your code, your issue seems to be in this expression: current % 10. The modulo operation gives you the remainder of a division by ten. In the case of your input 120, the result of that operation would be 0.

Following the rest of your application logic, your iteration variable will be set to zero, ending your loop immediately.

I will not give you copy-paste code to fix this problem, as it seems like a programming course assignment. I will however help you solve it.

My suggested fix is to change your approach to this problem and not try to solve this the mathematical way, but rather in a way that takes advantage of the Java programing language. You could change your input from an Integer to a String. In which case, you can use String.length() to ensure your requirement is fulfilled when exiting the loop. In your loop, you split the String into substrings of length 1. Afterwards, you just multiply these.

When the loop exits (because String length is no longer greater than 1) you will have your intended result.

Good luck!

Upvotes: 2

solid.py
solid.py

Reputation: 2812

I think you are looking something like the code below:

import java.util.Scanner;

public class Main {
    public static int numberOfProducts(String number) {
        int product = 1;
        do {
            for (int i = 0; i < number.length(); ++i){
                // This line converts the every digit of the number string to an integer.
                product *= number.charAt(i) - '0';
            }
            // Remove this line, if you don't want to print the product of each iteration.
            System.out.println(number);
            // Update number with the new product.
            // This line converts the int product to a new string.
            number = "" + product;
        } while (product > 10);
        return product;
    }
    public static void main(String[] args) {
        System.out.print("Please input the number: ");
        Scanner keyboard = new Scanner(System.in);
        int a = keyboard.nextInt();
        // Treat number as a string for easier indexing.
        String number = "" + a;
        System.out.println(numberOfProducts(number));
    }
}

When the above code runs, with 546 as input, it outputs:

Please input the number: 546
546
120
0

Upvotes: 2

WJS
WJS

Reputation: 40062

For a different take on the solution you can use a recursive lambda

import java.util.Scanner;
import java.util.function.IntFunction;

public class Product {
    // this simply reduces the number to the product of its digits.
    static IntFunction<Integer> prod =
            (a) -> a < 10 ? a : a % 10 * Product.prod.apply(a / 10);

    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);
        System.out.print("Please input the number");
        int n = keyboard.nextInt();

        // Now continue applying the lambda until the result is < 10.
        while (n > 10) {
            n = prod.apply(n);
        }
        System.out.println(n);
    }
}

Upvotes: 2

Related Questions