Q:

Write a C++ program to count all the numbers with unique digits within a given range 0 = y < 10n where y represent the unique digits numbers and take n as a input from the user

0

Write a C++ program to count all the numbers with unique digits within a given range 0 = y < 10n where y represent the unique digits numbers and take n as a input from the user

Sample Input: n = 1
Number of unique digits: 10
Sample Input: n = 2
Number of unique digits: 91

Sample Output:

n = 1, Number of unique digits: 10

n = 2, Number of unique digits: 91

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <iostream>
#include <cmath>

using namespace std;

int count_Unique_Digits_numbers(int n) {
        if (n == 0) {
            return 1;
        }
        int ctr = 10;
        for (int k = 2, fk = 9; k <= n; ++k) { 
            fk *= 10 - (k - 1);
            ctr += fk;
        }
        return ctr;  
    }
int main() 
{
    int n = 1;
    cout << "\nn = " << n << ", Number of unique digits: " << count_Unique_Digits_numbers(n) << endl;   
    n = 2;  
    cout << "\nn = " << n << ", Number of unique digits: " << count_Unique_Digits_numbers(n) << endl;       
	return 0;    
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now