Question:
how to write a program which count numbers of digits in a number?
saba
2014-01-04 06:57:36 UTC
like if i enter a number 53100149, program show there are 2 zeros, 2 ones, 1 three,1 four ,1 five and 1 nine.
Six answers:
2014-01-04 08:07:49 UTC
You could set aside storage for each digit, like 'Ones, Twos' etc... Then think about using normal division combined with the modulus divisor operator (%) in the C programming language to extract each digit.



There might be a more elegant solution, but this was quick using C++ ...



#include



using namespace std;



int main()

{

int num, digit = 0;

int zeros = 0, ones = 0, twos = 0, threes = 0, fours = 0;

int fives = 0, sixes = 0, sevens = 0, eights = 0, nines = 0;



cout << "Enter an Integer: ";

cin >> num;

cout << endl;



cout << "num: " << num << endl;



while(num > 0)

{

digit = num % 10;

num /= 10;



switch (digit)

{

case 0:

++zeros;

break;

case 1:

++ones;

break;

case 2:

++twos;

break;

case 3:

++threes;

break;

case 4:

++fours;

break;

case 5:

++fives;

break;

case 6:

++sixes;

break;

case 7:

++sevens;

break;

case 8:

++eights;

break;

case 9:

++nines;



}

}

cout << "Zeros: " << zeros << endl;

cout << "One's: " << ones << endl;

cout << "Two's: " << twos << endl;

cout << "Three's: " << threes << endl;

cout << "Four's: " << fours << endl;

cout << "Five's: " << fives << endl;

cout << "Six's: " << sixes << endl;

cout << "Sevens: " << sevens << endl;

cout << "Eights: " << eights << endl;

cout << "Nines: " << nines << endl;

return 0;

}
cja
2014-01-05 16:02:32 UTC
You're getting a lot of options in these answers, which is good. There are many ways to solve this problem, you'll have to decide which one suits you. A recursive function will give you a tidy solution. The function in my example depends on the caller to provide a properly initialized array of sufficient size, which is not ideal but is often what has to be done in C. The other limitation is that zero is a special case. It works, though, and has the benefit of doing the job in just a 3-line function.



#include

#include

#include



#define MAX_LINE_LEN 32

#define N 10



typedef enum { false = 0, true } bool;

int getInt(void);

void countDigits(int, int *);

const int dcInit[N] = { -1 };



int main(int argc, char *argv[]) {

    int digitCount[N], i, n;



    while (true) {

        memcpy(digitCount, dcInit, sizeof(digitCount));

        printf("\n\nEnter an integer: ");

        if ((n = getInt()) != 0) {

            countDigits(abs(n), digitCount);

            for (i = 0; i < N; i++) {

                if (digitCount[i] > 0) {

                    printf("[%d : %d] ", i, digitCount[i]);

                }

            }

        } else {

            puts("[0 : 1]");

        }

    }

    return 0;

}



void countDigits(int n, int *dc) {

    ++dc[n % 10];

    if (n == 0) return;

    countDigits(n / 10, dc);

}



int getInt() {

    static char line[MAX_LINE_LEN];

    static char junk[MAX_LINE_LEN];

    int n;

    bool inputOk = false;

 

    do {

        fgets(line, MAX_LINE_LEN, stdin);

        if (sscanf(line, "%d%s", &n, junk) == 1) {

            inputOk = true;

        } else {

            printf("invalid input; enter an integer\n> ");

        }

    } while (inputOk == false);

    return n;

}



#if 0



Sample run:



Enter an integer: 53100149

[0 : 2] [1 : 2] [3 : 1] [4 : 1] [5 : 1] [9 : 1]



Enter an integer: -2233

[2 : 2] [3 : 2]



#endif
2014-01-04 15:31:04 UTC
Well, first of all I would make the user input the number as a string or char* . Then I would make two arrays, most likely two-dimensional. In the first dimension of that array, store whatever numbers you found in the input string, but beware of duplicates. In your example, 53100149. So you should have an array that looks like this: { [ 5 , 0 ], [ 3 , 0 ], [ 1 , 0 ], [ 0 , 0 ], [ 4 , 0 ], [ 9 , 0 ] }. Then in the second dimension, you count and store the occurence of each character in the first dimension so it would be like:

{ [ 5 , 1 ], [ 3 , 1 ], [ 1 , 2 ], [ 0 , 2 ], [ 4 , 1 ], [ 9 , 1 ] }



That's how I think you should do it. As for actual code, well trust me it's better to learn it yourself. If you don't understand at all what I'm talking about, you should probably learn the basics first, like arrays, 2D arrays, and string manipulation.
jcastro
2014-01-04 23:47:36 UTC
Use a function that converts int to char*: itoa(), I guess. Treat the resulting char* as an array, use a for to search it, and have at hand a digit_count array of ints to keep the results.
Ali
2014-01-04 15:06:41 UTC
your question is too general

in what language do want to write the program?
?
2014-01-04 15:01:33 UTC
dont ask silly qns .i love u


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...