(Coderbyte) Multiplicative Persistence - Solução

Have the function MultiplicativePersistence(num) take the num parameter being passed which will always be a positive integer and return its multiplicative persistence which is the number of times you must multiply the digits in num until you reach a single digit. For example: if num is 39 then your program should return 3 because 3 * 9 = 27 then 2 * 7 = 14 and finally 1 * 4 = 4 and you stop at 4.

Solução:

import java.util.*;
import java.io.*;
import java.lang.*;

class Function { 
  int MultiplicativePersistence(int num) {
 
    String numero = String.valueOf(num);
    int contador = 0;
    while (numero.length() > 1) {
      int multiplicador = 1;
      for (int i = 0; i < numero.length(); i++) {
           multiplicador *= Integer.parseInt(Character.toString(numero.charAt(i)));
      }
      numero = String.valueOf(multiplicador);

      contador++;
    }
      
    return contador;

   
  }
 
  public static void main (String[] args) { 
    // keep this function call here    
    Scanner  s = new Scanner(System.in);
    Function c = new Function();
    System.out.print(c.MultiplicativePersistence(s.nextLine()));
  }  
 
}           

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução