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.
#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;
}
Comments
Leave a comment