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

This is a discussion on c++ program-the number of common digits of 2 numbers a & b within the C++ Programming forums, part of the General Programming Boards category; Hello guys! I have to make a program that displays the k number of common digits of 2 numbers a ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    26

    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?

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    26
    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?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-30-2010, 09:08 PM
  2. finding common numbers
    By BB89 in forum C Programming
    Replies: 14
    Last Post: 11-10-2009, 07:22 AM
  3. counting common multiples of 2 numbers
    By j0nnyX in forum C Programming
    Replies: 1
    Last Post: 10-26-2004, 06:20 PM
  4. Replies: 7
    Last Post: 05-26-2003, 05:44 PM
  5. Number system base M, print numbers N digits wide...
    By biterman in forum C Programming
    Replies: 12
    Last Post: 11-19-2001, 03:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21