N ডিজিট এবং বেস B দেওয়া আছে। বেস B-এ 0 থেকে B-1 পর্যন্ত ডিজিট থাকে। front শূন্য ছাড়া N ডিজিটের সংখ্যা গণনা করতে হবে।
মোট N ডিজিট সংখ্যা (শূন্য সহ) = B^N। প্রথম ডিজিট ০ হলে বাকি N-1 ডিজিট = B^(N-1)। তাই শূন্য ছাড়া মোট = B^N - B^(N-1)
অ্যালগরিদম
countNDigitNum(N, B)
Begin total := B^N with_zero := B^(N-1) return B^N - B^(N-1) End
উদাহরণ
#include <iostream> #include <cmath> using namespace std; int countNDigitNum(int N, int B) { int total = pow(B, N); int with_zero = pow(B, N - 1); return total - with_zero; } int main() { int N = 5; int B = 8; cout << "Number of values: " << countNDigitNum(N, B); }আউটপুট
Number of values: 28672