Calculate and show persistence of a whole [closed]

3

It's something of a project and I need to add this:

Calculate and show the persistence of a given whole number. The persistence of a Number is the number of times you have to multiply the digits of a number until it becomes a single-digit number.

In C language

    
asked by Enmanuel De Los Santos Cruz 07.11.2016 в 00:53
source

2 answers

2

I do not program in C, but an example that works in java and can help you to carry it is this (example for nº 294):

int num = 294;
int repeticiones = 0;

System.out.println("Nº inicial es: " + num);
while(num > 9){
    int producto = 1;
    while(num != 0){
        int unidades = num % 10;
        producto *= unidades;
        num = (num - unidades)/10;
    }

    repeticiones++;
    System.out.println("Producto " + repeticiones + "º es:" + producto);
    num = producto;
}

System.out.println("La persistencia es: " + repeticiones + ", la cifra final: " + num);

Give us this exit:

  

Initial number is: 294
  Product 1º is: 72
  Product 2º is: 14
  Product 3º is: 4
  The persistence is: 3, the final figure: 4

    
answered by 07.11.2016 в 13:44
2

I recently answered something similar.

Java function to find numbers

It is necessary to have a function that allows to obtain the digits.

Function taken from here.

link

#include <math.h>

int getDigit(int number, int k)
{
    return (number/(int)pow(10,k-1))%10;
}

So now we have to do that, as long as it has more than one digit, something happens. That is, as long as it is greater than or equal to 10.

Note: I copy something similar to what I had in the capicúa's question.

while(numero>=10)
{
    int acumular = 1; 
    int longitud = (int)log10(numero);
    for (int i = 0; i < longitud; i++)
    {
        acumular *= obtenerDigito(numero,i);
    }
    numero = acumular;
}

This works only for integers less than 2 raised to 31, that is, about 2 billion, due to the limitation of the architecture, that is, but I think it is more than enough.

    
answered by 07.11.2016 в 13:15