Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been asked to create a program in C in which will take an 2d array of eg. 3 numbers and will find the persistance of one number every time by multiplying it until we gat only one number. Then the result of each number will be filed in the second row of the array.

Eg.

For example, the persistence of the number 2736 is 0: first we find that 2 * 7 * 1 * 6 = 252, then that 2 * 5 * 2 = 20 and finally 2 * 0 = 0 where we come to a single digit. (which in that example is 0).

Now lets say we have that numbers in every row of the array :
2716 , 2717, 2718, 2719, 2720 

C
array[5][2] = { {2716,0}, {2717,0}, {2718,0}, {2719,0}, {2720,0} };


The graphical presentaion of every persistance would be

Number | Persist
-----------------
  2716  |    3
  2717  |    4
  2718  |    2
  2719  |    3
  2720  |    1


and the array after wil be :
C
array[5][2] = { {2716,3}, {2717,4}, {2718,2}, {2719,3}, {2720,1} };



I tried to create a code but i get stacked in two things.
First when I make the first multiplyzation I need to redo it cause the number cant be one digit (except in 2720 which the result is 0 from the first time)

and second I can't pass the persist result of each number into the array.

Here's my code so far:

C
#include <stdio.h> 
#include <conio.h> 

int main()
{
	// set 0 to every second row cause we don't know the persist of the 1 row number yet
	int array[5][2] = { {2716,0}, {2717,0}, {2718,0}, {2719,0}, {2720,0} };
	int a, b = 1;
	int number;
	
	for(int i =0; i < 5; i++)
	{
		number = array[i][0];
		
		for(;number != 0; number = number / 10)
		{
			a = number % 10;
			b = b * a ;
			array[i][1] = b;  //set array[i][1] cause we want to fill the 2nd column of the array
		}
		
	}
	
	
	for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 2; j++) {
            printf("%d ", array[i][j]);
        }
        printf("\n");
    }
   
}
Posted
Updated 16-Dec-14 3:15am
v2

1 solution

I would factor out the multiply-the-digits operation, namely:
C
#include <stdio.h>


int multiply_digits(int x)
{
  int z = 1;
  while ( x )
  {
    z *= x % 10;
    x /= 10;
  }

  return z;
}


int main()
{
  // set 0 to every second row cause we don't know the persist of the 1 row number yet
  int array[5][2] = { {2716,0}, {2717,0}, {2718,0}, {2719,0}, {2720,0} };

  int i, j;
  for (i=0; i<5; ++i)
  {
    int n = array[i][0];
    int j = 0;
    while (n>9)
    {
      n = multiply_digits(n);
      j++;
    }
    array[i][1] = j;
  }

  for (i = 0; i < 5; i++)
  {
    for (j = 0; j < 2; j++)
    {
       printf("%d ", array[i][j]);
    }
    printf("\n");
  }

  return 0;
}
 
Share this answer
 
Comments
[no name] 16-Dec-14 10:09am    
thank you sir. I also will adjust your code so I can use a dynamic array. OH I found a mistake. If we have to multiply numbers with 0 then the result should be 0! Please fix it, or did I understand the problem wrong?
[no name] 16-Dec-14 10:28am    
what if we have one digit number? The output is 0! is this what it need's to be?
CPallini 16-Dec-14 12:26pm    
Being the answer to 'how many steps are required ?', I think zero is the correct answer.
[no name] 16-Dec-14 13:10pm    
:Rip: Yeah sorry about that. i find the solution myshelf! Keep up the good work! :)
Sergey Alexandrovich Kryukov 16-Dec-14 10:35am    
5ed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900