Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,884 questions

51,810 answers

573 users

How to get the first 100 cyclops numbers (number with odd number of digits and zero in the center) in Java

1 Answer

0 votes
public class MyClass {
    private static boolean isCyclopsNumber(int n) {
    	if (n == 0) {
    		return true;
    	}
    
    	int m = n % 10;
    	int count = 0;
    	while (m != 0) {
    		count++;
    		n /= 10;
    		m = n % 10;
    	}
    
    	n /= 10;
    	m = n % 10;
    	while (m != 0) {
    		count--;
    		n /= 10;
    		m = n % 10;
    	}
    
    	return n == 0 && count == 0;
    }
    
    private static void getFirstCyclopsUpTo(int total) {
    	int n = 0;
    	int col = 0;
    
    	while (total > 0) {
    		if (isCyclopsNumber(n)) {
    			System.out.printf("%7d", n);
    			col++;
    			if (col == 10) {
    				System.out.print("\n");
    				col = 0;
    			}
    			total--;
    		}
    		n++;
    
    	}
    }

    public static void main(String args[]) {
      	getFirstCyclopsUpTo(100);
    }
}





/*
run:
 
      0    101    102    103    104    105    106    107    108    109
    201    202    203    204    205    206    207    208    209    301
    302    303    304    305    306    307    308    309    401    402
    403    404    405    406    407    408    409    501    502    503
    504    505    506    507    508    509    601    602    603    604
    605    606    607    608    609    701    702    703    704    705
    706    707    708    709    801    802    803    804    805    806
    807    808    809    901    902    903    904    905    906    907
    908    909  11011  11012  11013  11014  11015  11016  11017  11018
  11019  11021  11022  11023  11024  11025  11026  11027  11028  11029
 
*/



 



answered Mar 29, 2023 by avibootz
...