
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
All possible numbers of N digits and base B without leading zeros?
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 BN number of different N digit values can be generated (including leading 0s). The first digit is 0m if we ignore it there are BN-1 number. So total N digit numbers which has no leading 0 are BN – BN-1
Algorithm
countNDigitNum(N, B)
Begin total := BN with_zero := BN-1 return BN – BN-1 End
Example
#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); }
Output
Number of values: 28672
- Related Articles
- Write all possible 3- digit numbers (without repeating the digits) , by using the digits.(i)6,7,5 (ii) 9,0,2
- Compute sum of digits in all numbers from 1 to n
- Counting n digit Numbers with all unique digits in JavaScript
- Print all possible sums of consecutive numbers with sum N in C++
- Adding base n numbers
- Python Program to Accept Three Digits and Print all Possible Combinations from the Digits
- Golang Program to Read Three Digits and Print all Possible Combinations from the Digits
- All possible binary numbers of length n with equal sum in both halves?
- Remove leading zeros in array - JavaScript
- Add leading Zeros to Python string
- Java Program to Remove leading zeros
- Golang program to remove leading zeros
- How to Add or Pad Leading Zeros to Numbers or Text in Excel?
- Finding all the n digit numbers that have sum of even and odd positioned digits divisible by given numbers - JavaScript
- List all the three digit numbers can be made from the digits 0,5,9 without repetition.

Advertisements