Javatpoint Logo
Javatpoint Logo

Pandigital Number in Java

In this section, we will discuss what is pandigital number along with their versions and also create Java programs to check if the given number is a pandigital number or not. The pandigital number program frequently asked in Java coding interviews and academics.

Pandigital Number

A 10-digit integer number that has each digit (0 to 9) of its base, exactly once is known as a pandigital number. It is an OEIS sequence A050278.

Note that the base (number of digits) must be less than or equal to 36. The base 36 has digits 0 to 9 and characters A to Z.

There are the following versions of pandigital numbers:

Zero less Pandigital Number

A 9-digit number in which each digit (1 to 9) appears at least once is known as zero less pandigital number. It is an OEIS sequence A05028 9. It does not contain 0's. For example, 123456789, 123456798, 123456879, 123456897, 123456978, 123456987, 123457689, 123457698, 123457869, etc.

Prime Pandigital Number

A number that contains digits 0 to 9 is known as a pandigital number. In prime pandigital, digits may appear multiple times. It is an OEIS sequence A050288. For example, 10123457689, 10123465789, 10123465897, 10123485679, 10123485769, 10123496857, 10123547869, 10123548679, 10123568947, 10123578649, 10123586947, etc.

Base-8 Pandigital Prime

A pandigital number that has at least nine octal digits is known as base-8 pandigital prime. It is an OEIS sequence A175271. For example, 17119607, 17120573, 17121077, 17127839, 17128931, 17132347, 17135413, 17136029, 17136869, 17148349, 17159479, 17164757, etc.

Base-9 Pandigital Prime

A pandigital number that has at least ten digits in base 9 is known as base-9 pandigital prime. There must be at least one repeated digit, which may not be even, else the resulting number is even. It is an OEIS sequence A175280. For example, 393474749, 393474821, 393475373, 393481069, 393486901, 393488437, 393492797, 393494477, 393499429, 393499517, 393500741, 393528029, etc.

Pandigital Numbers (of sequence A050278) with product of two adjacent digits visible as a substring of the digits. It is an OEIS sequence A198298. For example, 3205486917, 3207154869, 4063297185, 4063792185, 4230567819, 4230915678, 4297630518, 4297631805, 5042976318, 5063297184, 5079246318, 5184063297, 5420796318, etc.

Take any number from the above (5042976318), we observe that the product of two adjacent digits appears in the number as a substring. For example, 4*2=8, 9*7=63, 6*3=18.

Similarly, Pandigital Number (of sequence A050278) with the product of three adjacent digits visible as a substring of the digits. It is an OEIS sequence A210013. For example,
5631890724, 6581324079, 6581324097, 7249056318.

Take any number from the above (5631890724), we observe that the product of three adjacent digits appears in the number as a substring. For example, 5*6*3=90, 7*2*4=56, 1*8*9=72.

There are two more pandigital sequences in OEIS A115927 and A054383.

Let's understand it through examples.

Pandigital Number Example

Suppose, we have taken the number 9172365480 and its base is 10.

We observe that the number contains all the digits from 0 to 9 and each digit appears exactly once. Hence, it is a pandigital number.

Let's take another number 103456879 it's base is 10.

The number contains 9-digits. It is not a pandigital number because the number has 9-digits and a digit is missing also.

Similarly, we can check also for another base. Let's take an example.

Consider the number 123456789ABCDEFGHIJKLMOPQRSTUVWXYZ whose base is 36. It is not a pandigital number because N is missing in the number.

If any number or letter is missing in a number whose base is 36 cannot a pandigital number.

Some other pandigital numbers are 1023456789, 1023456798, 1023456879, 1023456897, 1023456978, 1023456987, 1023457689, 1023457698, 1023457869, etc.

Pandigital Number Java Program

An integer N is given whose base is b. We have to check if the given number is a pandigital number in the specified base or not.

Note: The following program does not work for base-8 and base-9 Pandigital prime number.

PandigitalNumberExample.java

Output 1:

Enter the base of the number: 10
Enter the number: 0987654321
The given number is a pandigital number.

Output 2:

Enter the base of the number: 36
Enter the number: 2345609871ABCDEFGHIJKLMNOPQRSTUVWXYZ
The given number is a pandigital number.

Output 3:

Enter the base of the number: 13
Enter the number: 123456ABCD789
The given number is not a pandigital number.

Note: The sequence of alphabets and digits does not matter. Either all alphabets (A-Z) or all digits (0-9) may come first. The use of alphabets between digits or vice versa is not allowed. the number must be a 10-digit number. If we try to enter a 9-digit number, it shows ArrayIndexOutOfBoundsException.

Pandigital Number in Java





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA