Wednesday, March 5, 2008

Nested For-loop using Java

I took an exam from one of the Japanese company here in the Philippines for the programmer’s trainee position, and then the programming part is the same as this problem that you can use C or Java programming, depends to our knowledge.

I know that this problem is very easy, because I’ve encountered this problem when I was a first year college student using C language, but whyyyy I can’t answer this simple problem during that exam, well the result is failed, (lol), because of that incident, and because of my stupidity I tried this problem into my own computer, then I got it right (sad). I’m working for almost 8 months as a junior programmer, but then I didn’t pass that exam. (I think, because I didn’t get any proper training at all, or maybe I’m just really stupid), well there is always another chance :P.


1. Code: Try4.java

class Try4{

public static void main(String[] args){

for(int ctr=0;ctr<6;ctr++){

for(int ctr2=4;ctr2>=ctr;ctr2--){

System.out.print("* ");

}

for(int ctr3=0;ctr3<=ctr;ctr3++){

System.out.print("@ ");

}

System.out.println(" ");

}

for(int ct=0;ct<5;ct++){

for(int ct2=0;ct2<=ct;ct2++){

System.out.print("* ");

}

for(int ct3=4;ct3>=ct;ct3--){

System.out.print("@ ");

}

System.out.println(" ");

}

}

}






2. Code: Try3.java

class Try3{

public static void main(String[] args){

for(int ctr=0;ctr<10;ctr++){

for(int ctr2=8;ctr2>=ctr;ctr2--){

System.out.print("* ");

}

for(int ctr3=0;ctr3<=ctr;ctr3++){

System.out.print("@ ");

System.out.print(" ");

}

System.out.println(" ");

}

}

}




3. Code: Try2.java

class Try2{

public static void main(String[] args){

for(int ctr=0;ctr<10;ctr++){

for(int ctr2=1;ctr2<=ctr;ctr2++){

System.out.print(" ");

System.out.print(" ");

}

for(int ctr3=9;ctr3>=ctr;ctr3--){

System.out.print("* ");

}

System.out.println(" ");

}

}

}




4. Code: Try.java

class Try{

public static void main(String[] args){

for(int ctr=0;ctr<10;ctr++){

for(int ctr2=8;ctr2>=ctr;ctr2--){

System.out.print(" ");

System.out.print(" ");

}

for(int ctr3=0;ctr3<=ctr;ctr3++){

System.out.print("* ");

}

System.out.println(" ");

}

}

}




5. Code: Triangle.java

class Triangle{

public static void main(String[] args){

for(int ctr=0;ctr<10;ctr++){

for(int ctr2=9;ctr2>=ctr;ctr2--){

System.out.print(" ");

}

for(int ctr3=0;ctr3<=ctr;ctr3++){

System.out.print("@ ");

}

System.out.println(" ");

}

}

}




6. Code: Square2.java

class Square2{

public static void main(String[] args){

for(int ctr=0;ctr<10;ctr++){

for(int ctr2=0;ctr2<=ctr;ctr2++){

System.out.print("@ ");

}

for(int ctr3=9;ctr3>=ctr;ctr3--){

System.out.print("* ");

}

System.out.println(" ");

}

}

}




7. Code: Square.java

class Square{

public static void main(String[] args){

for(int ctr=0;ctr<10;ctr++){

for(int ctr2=9;ctr2>=ctr;ctr2--){

System.out.print("* ");

}

for(int ctr3=0;ctr3<=ctr;ctr3++){

System.out.print("@ ");

}

System.out.println(" ");

}

}

}




8. Code: Sample2.java

class Sample2{

public static void main(String[] args){

for(int ctr=0;ctr<10;ctr++){

for(int ctr2=9;ctr2>=ctr;ctr2--){

System.out.print("* ");

}

System.out.println(" ");

}

}

}




9. Code: Sample.java

class Sample{

public static void main(String[] args){

for(int ctr=0;ctr<10;ctr++){

for(int ctr2=0;ctr2<=ctr;ctr2++){

System.out.print("* ");

}

System.out.println(" ");

}

}

}