Answer to Question #172961 in C++ for Vijay Shiva

Question #172961

Write a function Unique_digit and the function to find the count of unique digits in a given number N. The number will be passed to the program as the input of type int.

Assumption: The input number will be a positive integer number>=1 and <= 25000.

For e.g.

If the given number is 292, the program should print 2 because there are only 2 unique digits "2" and "9" int this number.


1
Expert's answer
2021-03-18T12:49:01-0400
#include <iostream>
using namespace std;

int Unique_digits(int n) {
    int count[10] = {0};
    int unique = 0;

    while (n > 0) {
        int d = n%10;
        if (count[d] == 0) {
            count[d] = 1;
            unique++;
        }
        n /= 10;
    }
    return unique;
}

int main() {
    int n;
    cout << "Enter a number: ";
    cin >> n;

    cout << "It has " << Unique_digits(n) << " unique digits" << endl;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS