Smallest number with digits product of n

halu

I need to find the smallest number which digit numbers product is equal to a given num.

import java.util.Scanner;

class timus_1014_2 {

    public static void main(String[] args){
        int[] arr = new int[10]; // eskan ban@  chem imanum inchi a statik,
        int prod = 1;
        int j = 0;

        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        for (int i = 2; i < 10; ++i){
            if (n % i == 0) {
                arr[j] = i;
                j++;
            }
        }
        prod =  prod * arr[j];

        System.out.print(prod);

    }
}

Something is wrong with the logic, whats is the problem when I input 10 it should give 25 but it gives 0. Please give ideas of how to make a program find a number which digits product is a given num.

sujithvm

If I understood your problem correctly you need a number whose product of digits equals a number N. Since you asked for new algorithm , you can chck following code.

Logic:

Note : For number whose prime factors are less than 10

  1. Get all factors from 9 -> 2
  2. add to list
  3. print in reverse or use stack instead of list
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter number");
        int num = in.nextInt();

        List<Integer> lst = new ArrayList<>();

        for (int p = 9; p >= 2; p--) {
            while (num % p == 0) {
                num /= p;
                lst.add(p);
            }
        }

        String smallestNumber = "";
        for (int i = lst.size() - 1; i >= 0; i--) {
            smallestNumber = smallestNumber + lst.get(i);
        }
        System.out.println("Smallest number : " + smallestNumber);
    }
}

Output :

Enter number
10
Smallest number : 25

Enter number
144
Smallest number : 289

Enter number
12
Smallest number : 26

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find smallest number Q whose product of digits is N

From Dev

Find smallest number K , if exists, such that product of its digits is N. Eg:when N = 6, smallest number is k=16(1*6=6) and not k=23(2*3=6)

From Dev

Product, Sum, and Digits of a Number

From Dev

Product of all digits of an integer number C#

From Dev

Build smallest number larger than K using only allowed digits

From Dev

Project Euler # 8- Find biggest product of n adjacent digits in number. Code works only for some values of n

From Dev

Sorting an array by number of digits in each element from largest to smallest using loops java

From Dev

Count number of 1 digits in 11 to the power of N

From Dev

Format number to N significant digits in PHP

From Dev

How to split a number after n digits in javascript?

From Dev

for loop for 0 to n number with digits specification

From Java

Given an integer N. What is the smallest integer greater than N that only has 0 or 1 as its digits?

From Dev

Number of digits

From Dev

finding smallest and second smallest number

From Dev

Given number N eliminate K digits to get maximum possible number

From Dev

Find number of pairs of digits in a number N that add up to 10

From Dev

Find number of pairs of digits in a number N that add up to 10

From Dev

Given number N eliminate K digits to get maximum possible number

From Dev

C# number format: display n significant number of digits?

From Dev

(Javascript) Take Three Integers From User to Display Sum, Average, Product, Smallest, and Largest Number

From Dev

Fastest algorithm to find the largest palindrome that is the product of 2 numbers with the same number of digits

From Dev

How to extract an n digits number in different forms ( separated by a space) - PHP

From Java

Check if 1/n has infinite number of digits after decimal point

From Dev

Regex to use in JavaScript string with not more than n number of digits

From Dev

Generate a random string with n digits number (except 0 at begin) in Ruby

From Dev

PowerShell Script to Generate Large Volume (in millions) 'n' digits Unique Number

From Dev

How to round off double to have max n number of digits

From Dev

Python epsilon is not the smallest number

From Dev

Smallest permutation of given number

Related Related

  1. 1

    Find smallest number Q whose product of digits is N

  2. 2

    Find smallest number K , if exists, such that product of its digits is N. Eg:when N = 6, smallest number is k=16(1*6=6) and not k=23(2*3=6)

  3. 3

    Product, Sum, and Digits of a Number

  4. 4

    Product of all digits of an integer number C#

  5. 5

    Build smallest number larger than K using only allowed digits

  6. 6

    Project Euler # 8- Find biggest product of n adjacent digits in number. Code works only for some values of n

  7. 7

    Sorting an array by number of digits in each element from largest to smallest using loops java

  8. 8

    Count number of 1 digits in 11 to the power of N

  9. 9

    Format number to N significant digits in PHP

  10. 10

    How to split a number after n digits in javascript?

  11. 11

    for loop for 0 to n number with digits specification

  12. 12

    Given an integer N. What is the smallest integer greater than N that only has 0 or 1 as its digits?

  13. 13

    Number of digits

  14. 14

    finding smallest and second smallest number

  15. 15

    Given number N eliminate K digits to get maximum possible number

  16. 16

    Find number of pairs of digits in a number N that add up to 10

  17. 17

    Find number of pairs of digits in a number N that add up to 10

  18. 18

    Given number N eliminate K digits to get maximum possible number

  19. 19

    C# number format: display n significant number of digits?

  20. 20

    (Javascript) Take Three Integers From User to Display Sum, Average, Product, Smallest, and Largest Number

  21. 21

    Fastest algorithm to find the largest palindrome that is the product of 2 numbers with the same number of digits

  22. 22

    How to extract an n digits number in different forms ( separated by a space) - PHP

  23. 23

    Check if 1/n has infinite number of digits after decimal point

  24. 24

    Regex to use in JavaScript string with not more than n number of digits

  25. 25

    Generate a random string with n digits number (except 0 at begin) in Ruby

  26. 26

    PowerShell Script to Generate Large Volume (in millions) 'n' digits Unique Number

  27. 27

    How to round off double to have max n number of digits

  28. 28

    Python epsilon is not the smallest number

  29. 29

    Smallest permutation of given number

HotTag

Archive