# c++ program-the number of common digits of 2 numbers a & b

• 12-22-2011
Sky_Daughter
c++ program-the number of common digits of 2 numbers a & b
Hello guys!
I have to make a program that displays the k number of common digits of 2 numbers a and b.
For example if a is 23456 and b is 456900 then the number of common digits is 3(4,5 and 6).
Do I need an array that can hold the values of the digits?
I can remove the digits from the numbers and then put them in two arrays but I need help.
Is this the good way for my program or should I try something else?
• 12-22-2011
oogabooga
Define a 10 element int array, initialized to zero. Loop through digits of first number, incrementing corresponding element. Loop through digits of second number, reading corresponding element. If > zero, you have a match; decrement the element.
• 12-22-2011
Sky_Daughter
I've tried to solve it but I couldn't. Please take a look at my code :
Code:

```#include<iostream> using namespace std; int main() {         int dg1[10],dg2[10],a,b,i = 0,j = 0,k = 0;         cout  << "a = ";         cin >> a;         cout << "b = ";         cin >> b;                 while (a) {                 dg1[i] = a % 10; a/= 10;i++;         }                 while (b) {                 dg2[j] = b % 10; b/= 10; j++;         }         if (dg1[i] == dg2[j]) {                 k++;                 cout << k << "\n";         }                 return 0; }```
It's not working. What's the problem and how should I fix it?
• 12-22-2011
Elysia
You fail completely to understand the logic of a program. Furthermore, you add useless variables that do nothing.
Go write a proper flowchart to illustrate the logic of how such a program would work, then come back if you have any trouble.