×

Zygodromes in Java

Zygodrome is a positive number created by the same digits running non-trivially. A number is called a zygodrome if identical digits constantly occur together (in pairs). The Greek word "zyg" means "union" or "pair."

Example: 1

Input: int n = 88

Output: 88 is  Zygodrome.

Explanation

 For example, 88 the digit 8 occurs in pairs. Hence, the number 88 is Zygodrome.

Example: 2

Input: int n = 808

Output: 808 is not a Zygodrome.

Description:

In 808, the digit 8 is not occurring in pairs. There is a digit 0 between two eights. Hence, the number 808 is not Zygodrome.

Java Program to find Zygodromes using While loop

A while loop can be used. The while loop iterates through the provided number's digits, using an auxiliary array to determine whether the number is zygodrome or not.

Zygodromes.java

Import java . util . * ;
public class Zygodromes  
{  
public boolean isZygodromes ( int n )  
{  
int d [ ] = new int [ 10 ]  ;    
while ( n ! = 0 )  
{  
int countSameDig = 0 ;  
  
// find the current digit  
int currDig = n % 10 ;  
int temp = currDig ;   
while( currDig == temp )  
{  
countSameDig = countSameDig + 1 ;  
n = n / 10 ;  
temp = n % 10 ;  
}  
if ( countSameDig == 1 )  
{  
return false ;  
}  
d [ currDig ] = countSameDig ;   
countSameDig = 0 ;   
}  
return true ;  
}    
// execution of the program starts  in the main section
public static void main ( String s [ ] )   
{  
Zygodromes o1 = new Zygodromes ( ) ;  
int n1 = 99 ;  
boolean isZygodrome = o1 . isZygodromes ( n1 ) ;  
if ( isZygodrome )  
{  
System . out  . println ( " taken number from user " + n1 + " is a Zygodrome . " ) ;  
}  
else  
{  
System . out . println (" Given number " + n1 + " is not a Zygodrome ." ) ;  
}  
  
System . out . println ( ) ;  
  
// 2nd input  
n1 = 909 ;  
//  calling the isZygodromes function and storing the outcome  
isZygodrome = o1 . isZygodromes ( n1 ) ;  
if ( isZygodromes )  
{  
System . out . println ( " Given number " + n1 + " is a Zygodrome . " ) ;  
}  
else  
{  
System . out . println ( " Given  number " + n1 + " is not a Zygodrome . " ) ;  
}   
System . out . println ( ) ;  
// 3rd input from user
n1 = 1100 ;  
// calling  method isZygodromes and storing the result in variable   
isZygodrome = o1 . isZygodromes ( n1 ) ;  
if ( isZygodromes )  
{  
System . out . println ( " The number " + n1 + " is a Zygodrome . " ) ;  
}  
else  
{  
System . out . println ( "Given number " + n1 + " is not a Zygodrome . " ) ;  
}   
System . out . println ( ) ;   
//  4rth input from user
int n4 = 4224 ;  
// calling the method isZygodromes and storing the result in the variable 
isZygodrome = o1 . isZygodromes ( n4 ) ;  
if ( isZygodromes )  
{  
System . out . println ( " Given number " + n4 + " is a Zygodrome . " ) ;  
}  
else  
{  
System . out . println ( " Given number " + n4 + " is not a Zygodrome ." ) ;  
}  
  
System . out . println ( ) ;  
  
// 5TH INPUT FROM THE USER
int n5 = 442244 ;  
// invoking the method isZygodromes and storing the result  
isZygodrome = o1 . isZygodromes ( n5 ) ;  
if ( isZygodromes )  
{  
System . out . println ( "  Given number " + n5 + " is a Zygodrome . " ) ;  
}  
else  
{  
System . out .  println ( " Given number " + n5 + " is not a Zygodrome. " ) ;  
}   
}  
}  

Output

The number 99 is a Zygodrome.


The number 909 is not a Zygodrome.


The number 1100 is a Zygodrome.


The number 4224 is not a Zygodrome.


The number 442244 is a Zygodrome.

Complexity Analysis

According to complexity analysis, the programme uses two nested while-loops. However, the inner loop also reduces the input number by iterating over its digits. The program's temporal complexity is therefore O(d), where d is the total number of digits in the input number. A second array is used by the programme. The size of the auxiliary array is fixed, though. As a result, O is the program's space complexity (1).

Java Program to find Zygodromes using Strings

The input number can be changed into a string, and the current, next, and previous characters of the string can then be compared to determine whether or not they match. A single loop can be used to accomplish it.

import java.util.* ;
public class Main   
{  
public boolean isZygodromes ( int n )  
{  
String t = Integer . toString ( n ) ;  
t = ' ' + t + ' ';  
for ( int k = 1 ; k < t . length ( ) – 1 ; k++ )  
{  
if ( t . charAt ( k ) != t . charAt ( k - 1) && t . charAt ( k ) != t . charAt ( k + 1) )  
{  
return false ;  
}  
}  
return true ;  
}  
public static void main ( String s[ ] )   
{  
 
Main o1 = new Main ( ) ;
Scanner sc = new Scanner ( System . in ) ;
 System.out . println(" enter a number ");
int n1 = sc.nextInt() ;  
boolean isZygodrome = o1 . isZygodromes ( n1 ) ;  
if ( isZygodrome )  
{  
System . out . println ( " taken input " + n1 + " is a Zygodrome ." ) ;  
}  
else  
{  
System . out . println (" taken input " + n1 + " is not a Zygodrome ." ) ;  
}  
System . out . println ( ) ;  
System.out.println(" enter a number ");
int n2 = sc.nextInt() ;  
isZygodrome = o1.isZygodromes(n2);  
if(isZygodrome)  
{  
System.out.println(" taken input " + n2 + " is a Zygodrome.");
}  
else  
{  
System.out.println(" taken input " + n2 + " is not a Zygodrome.");  
}   
System . out . println ( ) ;  
System . out . println ( " enter a number " ) ;
int n3 = sc . nextInt ( ) ;  
isZygodrome = o1.isZygodromes(n3);  
if ( isZygodrome )  
{  
System . out . println ( " taken input " + n3 + " is a Zygodrome. " ) ;  
}  
else  
{  
System.out.println(" taken input " + n3 + " is not a Zygodrome.");  
}    
System.out.println();   
System.out.println(" enter a number ");
int n4 = sc.nextInt() ;
isZygodrome = o1.isZygodromes(n4) ;  
if(isZygodrome)  
{  
System . out . println ( " taken input " + n4 + " is a Zygodrome. " ) ;  
}  
else  
{  
System . out . println ( " taken input " + n4 + " is not a Zygodrome ." ) ;  
}    
System . out . println ( ) ;    
// 5th input  
System.out.println(" enter a number ");
int n5 = sc.nextInt() ; 
isZygodrome = o1 . isZygodromes ( n5 ) ;  
if ( isZygodrome )  
{  
System . out . println ( " taken input " + n5 + " is a Zygodrome ." ) ;  
}  
else  
{  
System . out . println ( " taken input " + n5 + " is not a Zygodrome ." ) ;  
}
}
}

Output

taken input 99 is a Zygodrome .


taken input 808 is not a Zygodrome .


taken input 1210 is not a Zygodrome.
taken input 2211 is a Zygodrome.
taken input 221166 is a Zygodrome.

Related Topics

Types of Statements in Java

In natural languages, statements and sentences are roughly equivalent. In general, statements are similar to valid English sentences. We will talk about a statement in Java and the different kinds...

11 minutes read.

Java delete directory

The File classes in Java may symbolize a directory or a file on the system. Inside the java.io package, the Files class is accessible. The File class has several helpful...

2 minutes read.

Java String equalsIgnoreCase() method

equalsIgnoreCase() method compares two Strings based on the their content but ignore the case. Syntax public boolean equalsIgnoreCase(Objects anObject) Parameter anObject: Object to be compared with the current String without case consideration. Returns It returns true...

1 minute read.

How to Create an Object in Java

How to Create an Object in Java An object can be defined as a run time entity that contains the blue printof the class. It means that all the member functions...

5 minutes read.

Awesome explanation of Strings in Java

What do you mean by String? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

How to Develop Programming Logic in Java?

Introduction In the world of software development, Java programming language is one of the most powerful programming languages that is used to create a wide range of applications. It includes desktop,...

18 minutes read.

How to Convert long to int in Java

How to Convert long to int in Java When we assign a larger type value to a variable of smaller type, then we need to perform explicit casting for the conversion....

2 minutes read.

Graphics Program in Java

Graphics Program in Java The graphics program in Java is part of the Swing program in Java. In this section, we will learn about the implementation of custom graphics using some...

6 minutes read.

ArrayDeque in Java

ArrayDeque The ArrayDeque is one of the essential concepts in java to implement the deque interface. It will allow us to apply a resizable array to implement the Deque interface. This...

8 minutes read.

Various Operation on Queue using Linked List in Java

We keep track of front and rear pointers in a queue data structure. The head of the line points to the first item, and the back to the last. Rear is...

3 minutes read.

Java Identifiers

In Java, the symbolic notations used for identification are called identifiers. Identifiers can be the name for the class, variable name declaration, name of the package, constant name and many...

3 minutes read.

Java Database Connectivity with Oracle

JDBC: A Programmer can develop a complete application using the Java built-in API’s. So, for storing the data required for solving a real-world problem is stored into a database. To connect...

5 minutes read.

Java String hashCode() method

Java String hashCode() method returns hash code for current String. hash code for string object is computed as s[0]*31^(n - 1) + s[1]*31^(n - 2) + ... + s[n - 1] Using int...

2 minutes read.

Java float vs double

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

Java Math sqrt() Method

The sqrt() method of Java Math class returns the accurately rounded positive square root of the specified double value. Syntax: public static double sqrt(double a) Parameters: The parameter ‘a’ represents the number whose square...

2 minutes read.

Java Console

If there is a character-based console device connected to the active Java virtual machine, it can be accessed using methods provided by the Java.io.Console class. JDK 6 adds the Console...

3 minutes read.

Diamond problem in Java

The Diamond Problem in Java is connected to multiple inheritances. It is also referred to as the "deadly diamond dilemma" or even the "deadly diamond of death”. The solution for...

5 minutes read.

How to sort a String in Java

Sorting is the process of putting the elements in a certain order, either ascending or descending. Mostly the alphabetical order or natural order is used for a string. In other...

5 minutes read.

How to check the Java version in cmd

To make programs that can run on our systems, we need to install programming language-related software in our systems. Different programming languages require different types of software, aka IDEs (Integrated...

5 minutes read.

Leap Year Program in Java

Leap Year Program in Java: A leap year is a calendar year that have an extra day i.e. 366 days instead of 365 days is called leap year. In other...

2 minutes read.