What are pandigital numbers. Approach to find the pandigital Numbers using C++


Pandigital Number − In Mathematics, a Pandigital number is an integer that in a given base has among its significant digits each digit used in the base at least once.

Pandigital numbers are the integers in which each digit is used as the base at least one time.

For example, 1245678 is a pandigital number.

Approach to solve this problem

  • Take Input a number and a base.

  • Check the base if it is less than 2 and greater than 10 then return 1 otherwise check the number if it is pandigital or not.

  • An Integer function is_pandigital(long long n, int base) takes a number and a base as an input.

  • Count for all digits present in the number and increment the count.

  • Iterate over all the digits to see if there is an empty one, so return false.

  • An Integer function is_zeroless (long long n, int base) takes input and number and its base as Input and returns if the digit is found.

  • Iterating over all the digits find if there is any digit that is empty then will return 0.

  • Finally, the function check_number(long long number, int base) takes the number and its base as the input. The function returns the 1 if find otherwise 0.

Example

#include <iostream.h>"
using namespace std;
int is_pandigital(long long number, int base);
int is_zeroless_pandigital(long long number, int base);
int check_number(long long number, int base);
int main(){
   long long number;
   int base;
   cin>>"Enter a number";
   cout<<number;
   cin>>"Enter Base";
   cout<<base;
   if(base < 2 || base > 10){
      return 1;
   }
   if(check_number(number, base)){
      if(is_pandigital(number, base)){
         cout<<number<<""<<base;
      }
      else{
         cout<< number<< "is not a pandigital number in base"<<base;
      }
      if(is_zeroless_pandigital(number, base)){
         cout<<number<< "is a zeroless pandigital number in base"<<base;
      }
      else{
         cout<<number<< "is not a zeroless pandigital number in base" <<base;
      }
   }
   else{
      cout<<number<< "is not a valid number in base"<<base;
   }
   return 0;
}
int is_pandigital(long long number, int base){
   /* define an array to store the count of all digits */
   int digits[10], i;
   memset(digits, 0, sizeof(int)*10);
   /* for every digit in number, then increment count by one */
   while(number > 0){
      int digit = number % 10;
      ++digits[digit];
      number /= 10;
   }
   /* iterate over all the digits to see if there's an empty one, if so return false */
   for(i = 0; i < base; ++i)
      if(digits[i] == 0)
         return 0;
   /* if no empty digit found, return true */
   return 1;
}
int is_zeroless_pandigital(long long number, int base){
   /* define an array to store the count of all digits */
   int digits[10], i;
   memset(digits, 0, sizeof(int)*10);
   /* for every digit in number, increment count by one */
   while(number > 0){
      int digit = number % 10;
      if(digit == 0) return 0;
         ++digits[digit];
      number /= 10;
   }
   /* iterate over digits to see if there's an empty one, if so return false */
   for(i = 1; i < base; ++i)
   if(digits[i] == 0)
      return 0;
   /* if no empty digit found, return true */
   return 1;
}
/* This function checks if given number is valid in the given base */
int check_number(long long number, int base){
   while(number > 0){
      int digit = number % 10;
      if(digit > base - 1) return 0;
         number /= 10;
   }
   return 1;
}

Output

Running the above code will generate the following output −

Enter a number: 45
Enter base(min:2 to max-10):10
45 is not a valid number.

Updated on: 05-Feb-2021

330 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements