Вы находитесь на странице: 1из 7

1 | P a g e

1 | P a g e Copyright © Manikandan S http://www.smanikandan.co.nr 2009 Free for Use
1 | P a g e Copyright © Manikandan S http://www.smanikandan.co.nr 2009 Free for Use
1 | P a g e Copyright © Manikandan S http://www.smanikandan.co.nr 2009 Free for Use

Copyright © Manikandan S http://www.smanikandan.co.nr 2009 Free for Use

2 | P a g e

2 | P a g e This document consists of puzzles and programming problems, which I’ve

This document consists of puzzles and programming problems, which I’ve found in the Internet. The aim of this document is to promote logical thinking and reasoning, which are the essential components for any application developer. Since, Java is a pure object oriented language, I thought it would be a good language to start with. The solutions to the programming problems and puzzles have been included as a separate document. I recommend you to look at the solutions only after you’ve really tried your hand at solving the problems. All the Best. Enjoy Programming.

at solving the problems. All the Best. Enjoy Programming. Cheers, Manikandan S Copyright © Manikandan S
at solving the problems. All the Best. Enjoy Programming. Cheers, Manikandan S Copyright © Manikandan S

Cheers,

Manikandan S

Copyright © Manikandan S http://www.smanikandan.co.nr 2009 Free for Use

3 | P a g e

1. A Java program to print the following pattern

1

2

5

3 4 6 7 8 9

Solution:

class Pattern

{

public static void main(String args[])

{

int i,j,k=2;

System.out.println("1");

for(i=0;i<=3;i=i+2)

{

for(j=1;j<=i+3;j++)

{

System.out.print(k+" "); k++;

}

System.out.println();

}

}

}

Copyright © Manikandan S http://www.smanikandan.co.nr 2009 Free for Use

4 | P a g e

2. A Java program to print the following pattern

1

2

3

4

5 6

7

8 9 10

Solution:

class Pattern1

{

public static void main(String args[])

{

int i,j,k=1;

for(i=1;i<=4;i++)

{

for(j=1;j<=i;j++)

{

System.out.print((k++)+" ");

}

System.out.println();

}

}

}

Copyright © Manikandan S http://www.smanikandan.co.nr 2009 Free for Use

5 | P a g e

3. A Java program to print the following pattern

Solution:

1

2

3

4

5

6 8

7 9

class NumberPattern

{

 

public static void main(String args[])

{

for(int i = 1; i <= 5; i++)

{

int j = 0; for(int k = 0; k < i; k++)

{

System.out.print((i + j) + " "); j += 4 - k;

}

System.out.println();

}

}

}

Copyright © Manikandan S http://www.smanikandan.co.nr 2009 Free for Use

6 | P a g e

4. A Java program to print the following pattern

Solution:

1

0

1

1

0 1

0

1 0 1

1

0 1 0 1

0

1 0 1 0 1

1

0 1 0 1 0 1

Additional Information: This number pattern is actually a mathematical pattern called Floyds triangle.

class floyd

{

public static void main(String args[])

{

int i,j,k;

for(i=1;i<=7;i++)

{

if(i%2!=0)

{

for(j=1;j<=i;j++)

{

if(j%2==0)

{

System.out.print("0 ");

}

else

{

System.out.print("1 ");

}

Copyright © Manikandan S http://www.smanikandan.co.nr 2009 Free for Use

7 | P a g e

}

}

else if(i%2==0)

{

for(j=1;j<=i;j++)

{

 

if(j%2==0)

{

System.out.print("1 ");

}

else

{

System.out.print("0 ");

}

}

}

System.out.println();

}

}

}

Copyright © Manikandan S http://www.smanikandan.co.nr 2009 Free for Use