*78*

# Undulating Number in Java

In this section, we will learn **what is an undulating number** and also create **Java programs** to check if the given number is a undulating number or not. The **undulating number program** is frequently asked in Java coding interviews and academics.

## Undulating Number

An **undulating number** is a number that has only two types of digits and the alternate digits are same, i.e., it must be of the form “ABABAB….” when it is in the base 10 number system.

It is sometimes restricted to non-trivial undulating numbers which are required to have at least **three** digits and A is not equal to B.

### Undulating Number Example

*The first few undulating numbers are:*

*101, 121, 131, 141, 151, 161, 171, 181, 191, 202, 212, 232, 242, 252, 262, 272, 282, 292, 303, 313, 323, 343, 353, 363, 373, 383, 393, 404, 414, 424, 434, 454, 464, 474, 484, 494, …*

*Some larger undulating numbers are: 1010, 6363, 80808, 171717, 989898989.*

## Properties of an Undulating Number

- There are infinite amount of undulating numbers.
- For any n >= 3, there are 9 × 9 = 81 non-trivial n-digit undulating numbers, since the first digit can have 9 values (it cannot be 0), and the second digit can have 9 values when it must be different from the first.
- Every undulating number with even number of digits and at least four digits is composite, since: ABABAB…AB = 10101…01 × AB.

For example, 171717 = 10101 × 17. - Undulating numbers with odd number of digits are palindromic. They can be prime.

For example, 151. - If an undulating number is ABAB…AB with
*n*repetitions of AB, it can be expressed as AB × (10^{2n}– 1)/99.

For example, 171717 = 17 × (10^{6}– 1) / 99. - If an undulating number is ABAB…ABA with
*n*repetitions of AB followed by one A, it can be expressed as (AB × 10^{2n+1}– BA) / 99.

For example, 80808 = (80 * 10^{5}– 08) / 99

## Undulating Number Java Program

Consider the following example in which we have checked whether the given

number is undulating number or not.

**TestUndulatingNumber.java**

**Output 1:**

**Output 2:**

In this way, we have learned about undulating number and Java program to

check whether given number is undulating or not.