# Java Program to Check Cyclic Number

In the previous article, we have discussed Java Program to Check Cube Number

In this article we will see how to check a number is cyclic number in java or not and cyclo number in java.

## Java Program to Check Cyclic Number

Before jumping into the program directly, let’s first know what is this cyclic number.

Cyclic numbers are the numbers where the cyclic permutations of the digits are the successive multiple of the number. In more simpler way we can say when the number multiplied with 1,2,3,4,5,6,….n then the produced result contains the same digits as like the original number just in different orders.

    Example-1:

Number=142857 (Cyclic Number)

142857*1 = 142857
142857*2 = 285714
142857*3 = 428571
142857*4 = 571428
142857*5 = 714285
142857*6 = 857142

Example-2:
Number=12345 (Not Cyclic Number)
12345*1=12345
12345*2=24690
12345*3=37035
12345*4=49380
12345*5=61725


In the above, two examples we observed that when 142857 is multiplied with 1,2,3,4,5,6… the produced result contains the same digits only like the actual number 142857, only indifferent orders. So, 142857 is a cyclic number. Where as we saw the number 12345 when multiplied with 1,2,3,4,5… the digits in the produced result each time is different means contains different digits than the actual number 12345. So,12345 is not a cyclic number.

Let’s see different ways to check cyclic number.

Approach:

1. Enter/Declare a number as String.
2. Based on cyclic number condition we will check it is equal to 99999999 or not after multiplying the number with it’s length+1.
3. If satisfies condition then it is cyclic number.

### Method-1: Java Program to Check Cyclic Number By Using Static Input

import java.util.Scanner;
import java.math.BigInteger;

public class Example19
{

public static void main( String args[] )
{

//A number entered in string format
//and stored in variable num
String num = "142857";

//number stored in a variable tempnum
BigInteger tempnum = new BigInteger(num);

//length conatins number length+1
//which is in integer
int len = num.length()+1;
//that length assigned to str
//which is string variable
String str = String.valueOf(len);
//length contained in BigInteger var
BigInteger len1 = new BigInteger(str);
//StringBuilder object created
StringBuilder buffer = new StringBuilder();
for(int i = 0 ; i < (len-1); i++) {
buffer.append('9');
}

//buffer converted to StringBuilder to BigInteger
BigInteger buffernum = new BigInteger(buffer.toString());

//Cyclic number condition checked
//based on the properties of cyclic number
if(tempnum.multiply(len1).equals(buffernum)) {
System.out.println("It is a cyclic number.");
}
else {
System.out.println("Not a cyclic number.");
}
}
}
Output:

It is a cyclic number.

### Method-2: Java Program to Check Cyclic Number By Using User Input

import java.util.Scanner;
import java.math.BigInteger;

public class Example19
{

public static void main( String args[] )
{
//Scanner class object created
Scanner sc = new Scanner( System.in );
//A number entered and stored in variable num
System.out.println("Enter a number: ");
String num = sc.nextLine().trim();
//number stored in a variable tempnum
BigInteger tempnum = new BigInteger(num);

//length conatins number length+1
//which is in integer
int len = num.length()+1;
//that length assigned to str
//which is string variable
String str = String.valueOf(len);
//length contained in BigInteger var
BigInteger len1 = new BigInteger(str);
//StringBuilder object created
StringBuilder buffer = new StringBuilder();
for(int i = 0 ; i < (len-1); i++) {
buffer.append('9');
}

//buffer converted to StringBuilder to BigInteger
BigInteger buffernum = new BigInteger(buffer.toString());

//Cyclic number condition checked
//based on the properties of cyclic number
if(tempnum.multiply(len1).equals(buffernum)) {
System.out.println("It is a cyclic number.");
}
else {
System.out.println("Not a cyclic number.");
}
}
}
Output:

Case-1

Enter a number: 142857
It is a cyclic number.

Case-2

Enter a number: 142856
It is a cyclic number.

### Method-3: Java Program to Check Cyclic Number By Using User Defined Method

import java.util.Scanner;
import java.math.BigInteger;

public class Example19
{

public static void main( String args[] )
{
//Scanner class object created
Scanner sc = new Scanner( System.in );
//A number entered and stored in variable num
System.out.println("Enter a number: ");
String num = sc.nextLine().trim();
//checkNumber() method called to check Cyclic number
checkNumber(num);
}

//user defined method to check cyclic number
public static void checkNumber(String num)
{
//number stored in a variable tempnum
BigInteger tempnum = new BigInteger(num);

//length conatins number length+1
//which is in integer
int len = num.length()+1;
//that length assigned to str
//which is string variable
String str = String.valueOf(len);
//length contained in BigInteger var
BigInteger len1 = new BigInteger(str);
//StringBuilder object created
StringBuilder buffer = new StringBuilder();
for(int i = 0 ; i < (len-1); i++) {
buffer.append('9');
}

//buffer converted to StringBuilder to BigInteger
BigInteger buffernum = new BigInteger(buffer.toString());

//Cyclic number condition checked
//based on the properties of cyclic number
if(tempnum.multiply(len1).equals(buffernum)) {
System.out.println("It is a cyclic number.");
}
else {
System.out.println("Not a cyclic number.");
}
}
}
Output:

Case-1

Enter a number: 142857
It is a cyclic number.

Case-2

Enter a number: 142856
It is a cyclic number.

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Recommended Reading On: Java Program to Check if an Array of Integers without 0 and 1

Practice yourself:

1. Write A Program To Check If 2 Numbers Given As Input Are Cyclic Or Not.

Related Java Programs: