- Related Questions & Answers
- Average numbers in array in C Programming
- C Program to Multiply two Floating Point Numbers?
- C Program to Check if all digits of a number divide it
- Sum of the numbers up to N that are divisible by 2 or 5 in c programming
- Sum of first n natural numbers in C Program
- C Program for Print individual digits as words without using if or switch.
- Print first k digits of 1/n where n is a positive integer in C Program
- Sum of squares of first n natural numbers in C Program?
- Sum of squares of the first n even numbers in C Program
- Find all the numbers in a string using regular expression in Python
- Lambda expression in Python to rearrange positive and negative numbers
- HTML DOM Base target Property
- HTML DOM Base object
- HTML DOM Base href Property
- How do you separate zeros from non-zeros in an integer array using Java?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Here we will see one problem, We have N and base B. Our task is to count all N digit numbers of base B without any leading 0s. So if N is 2 and B is 2 there will be four numbers 00, 01, 10, 11. So only two of them are valid for this section. These are 10, 11, there are no leading 0s.

If the base is B, then there are 0 to B – 1 different digits. So B^{N} number of different N digit values can be generated (including leading 0s). The first digit is 0m if we ignore it there are B^{N-1} number. So total N digit numbers which has no leading 0 are B^{N} – B^{N-1}

Begin total := B^{N}with_zero := B^{N-1}return BN – 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

Advertisements