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

1|P age

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

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.

Cheers,
Manikandan S

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

1. A Java program to print the following pattern


1
234
56789
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 age

2. A Java program to print the following pattern


1
23
456
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 age

3. A Java program to print the following pattern


1
25
368
479
Solution:

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 age

4. A Java program to print the following pattern


1
01
101
0101
10101
010101
1010101
Solution:

Additional Information: This number pattern is actually a mathematical pattern called Floyd’s
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 age

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

Вам также может понравиться