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
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 :
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:
#include <stdio.h>
#include <conio.h>
int main()
{
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; }
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", array[i][j]);
}
printf("\n");
}
}