TechSparx Java Tuitions ICSE/JavaSolutionsBank-Patterns-1.0...Java Solutions Bank-Patterns 2010 2...

26
TechSparxJavaTuitionsTechSparxJav aTuitionsTechSparxJavaTuitionsTech SparxJavaTuitionsTechSparxJavaTuiti onsTechSparxJavaTuitionsTechSparx JavaTuitionsTechSparxJavaTuitionsT echSparxJavaTuitionsTechSparxJava TuitionsTechSparxJavaTuitionsTechS parxJavaTuitionsTechSparxJavaTuiti onsTechSparxJavaTuitionsTechSparx JavaTuitionsTechSparxJavaTuitionsT echSparxJavaTuitionsTechSparxJava TuitionsTechSparxJavaTuitionsTechS parxJavaTuitionsTechSparxJavaTuiti onsTechSparxJavaTuitionsTechSparx JavaTuitionsTechSparxJavaTuitionsT echSparxJavaTuitionsTechSparxJava TuitionsTechSparxJavaTuitionsTechS parxJavaTuitionsTechSparxJavaTuiti TechSparx Java Tuitions “Better than a thousand days of diligent study is one day with a Great Teacher” Java Solutions Bank Patterns (version 1.0) http://techsparx.webs.com/ SaravananG

Transcript of TechSparx Java Tuitions ICSE/JavaSolutionsBank-Patterns-1.0...Java Solutions Bank-Patterns 2010 2...

TechSparxJavaTuitionsTechSparxJav

aTuitionsTechSparxJavaTuitionsTech

SparxJavaTuitionsTechSparxJavaTuiti

onsTechSparxJavaTuitionsTechSparx

JavaTuitionsTechSparxJavaTuitionsT

echSparxJavaTuitionsTechSparxJava

TuitionsTechSparxJavaTuitionsTechS

parxJavaTuitionsTechSparxJavaTuiti

onsTechSparxJavaTuitionsTechSparx

JavaTuitionsTechSparxJavaTuitionsT

echSparxJavaTuitionsTechSparxJava

TuitionsTechSparxJavaTuitionsTechS

parxJavaTuitionsTechSparxJavaTuiti

onsTechSparxJavaTuitionsTechSparx

JavaTuitionsTechSparxJavaTuitionsT

echSparxJavaTuitionsTechSparxJava

TuitionsTechSparxJavaTuitionsTechS

parxJavaTuitionsTechSparxJavaTuiti

TechSparx Java Tuitions

“Better than a thousand days of diligent study is one day

with a Great Teacher”

Java Solutions Bank – Patterns

(version 1.0) http://techsparx.webs.com/

SaravananG

Java Solutions Bank-Patterns 2010

2 TechSparx Java Tuitions - 9880 205065

Contents

No table of contents entries found.

Java Solutions Bank-Patterns 2010

3 TechSparx Java Tuitions - 9880 205065

public class Pattern01

{

public static void main(String[] args)

{

System.out.println("First Pattern ");

printPattern01();

System.out.println("\nSecond Pattern ");

printPattern02();

System.out.println("\nThird Pattern ");

printPattern03();

System.out.println("\nFourth Pattern ");

printPattern04();

System.out.println("\nFifth Pattern ");

printPattern05();

}

/*

*

* *

* * *

* * * *

* * * * *

*/

public static void printPattern01()

{

int n = 5;

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

{

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

{

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

}

System.out.println();

}

}

/*

1

1 1

1 1 1

1 1 1 1

1 1 1 1 1

*/

public static void printPattern02()

{

int n = 5;

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

{

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

{

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

}

System.out.println();

}

}

Java Solutions Bank-Patterns 2010

4 TechSparx Java Tuitions - 9880 205065

/*

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

*/

public static void printPattern03()

{

int n = 5;

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

{

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

{

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

}

System.out.println();

}

}

/*

1

2 2

3 3 3

4 4 4 4

5 5 5 5 5

*/

public static void printPattern04()

{

int n = 5;

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

{

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

{

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

}

System.out.println();

}

}

Java Solutions Bank-Patterns 2010

5 TechSparx Java Tuitions - 9880 205065

/*

5

4 4

3 3 3

2 2 2 2

1 1 1 1 1

*/

public static void printPattern05()

{

int n = 5;

int number = n;

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

{

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

{

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

}

number--;

System.out.println();

}

}

}

/*

First Pattern

*

* *

* * *

* * * *

* * * * *

Second Pattern

1

1 1

1 1 1

1 1 1 1

1 1 1 1 1

Third Pattern

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

Fourth Pattern

1

2 2

3 3 3

4 4 4 4

5 5 5 5 5

Fifth Pattern

5

4 4

3 3 3

2 2 2 2

1 1 1 1 1

*/

Java Solutions Bank-Patterns 2010

6 TechSparx Java Tuitions - 9880 205065

public class Pattern02

{

public static void main(String[] args)

{

System.out.println("Eleventh Pattern ");

printPattern11();

System.out.println("\Twelfth Pattern ");

printPattern12();

System.out.println("\nThirteenth Pattern ");

printPattern13();

System.out.println("\nFourteenth Pattern ");

printPattern14();

System.out.println("\nFifteenth Pattern ");

printPattern15(); }

/*

* * * * *

* * * *

* * *

* *

*

*/

public static void printPattern11()

{

int n = 5;

for(int i = n; i >= 1; i--)

{

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

{

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

}

System.out.println();

}

}

/*

2 2 2 2 2

2 2 2 2

2 2 2

2 2

2

*/

public static void printPattern12()

{

int n = 5;

for(int i = n; i >= 1; i--)

{

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

{

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

}

System.out.println();

}

}

Java Solutions Bank-Patterns 2010

7 TechSparx Java Tuitions - 9880 205065

/* 1 2 3 4 5

1 2 3 4

1 2 3

1 2

1 */

public static void printPattern13()

{

int n = 5;

for(int i = n; i >= 1; i--)

{

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

{

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

}

System.out.println();

}

}

/* 5 5 5 5 5

4 4 4 4

3 3 3

2 2

1 */

public static void printPattern14()

{

int n = 5;

for(int i = n; i >= 1; i--)

{

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

{

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

}

System.out.println();

}

}

/* 1 1 1 1 1

2 2 2 2

3 3 3

4 4

5 */

public static void printPattern15()

{

int n = 5;

int number = 1;

for(int i = n; i >= 1; i--)

{

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

{

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

}

number++;

System.out.println();

}

}

}

Java Solutions Bank-Patterns 2010

8 TechSparx Java Tuitions - 9880 205065

/*

Eleventh Pattern

* * * * *

* * * *

* * *

* *

*

Twelfth Pattern

2 2 2 2 2

2 2 2 2

2 2 2

2 2

2

Thirteenth Pattern

1 2 3 4 5

1 2 3 4

1 2 3

1 2

1

Fourteenth Pattern

5 5 5 5 5

4 4 4 4

3 3 3

2 2

1

Fifteenth Pattern

1 1 1 1 1

2 2 2 2

3 3 3

4 4

5

*/

Java Solutions Bank-Patterns 2010

9 TechSparx Java Tuitions - 9880 205065

public class Pattern03

{

public static void main(String[] args)

{

System.out.println("Twenty First Pattern ");

printPattern21();

System.out.println("\nTwenty Second Pattern ");

printPattern22();

System.out.println("\nTwenty Third Pattern ");

printPattern23(); }

/*

*

* *

* * *

* * * *

* * * * *

* * * *

* * *

* *

*

*/

public static void printPattern21()

{

int n = 5;

//Loop to print the upper half

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

{

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

{

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

}

System.out.println();

}

//Loop to print the lower half

for(int i = n-1; i >= 1; i--)

{

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

{

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

}

System.out.println();

}

}

Java Solutions Bank-Patterns 2010

10 TechSparx Java Tuitions - 9880 205065

/*

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

1 2 3 4

1 2 3

1 2

1

*/

public static void printPattern22()

{

int n = 5;

//Loop to print the upper half

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

{

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

{

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

}

System.out.println();

}

//Loop to print the lower half

for(int i = n-1; i >= 1; i--)

{

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

{

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

}

System.out.println();

}

}

/*

1

2 2

3 3 3

4 4 4 4

5 5 5 5 5

4 4 4 4

3 3 3

2 2

1

*/

public static void printPattern23()

{

int n = 5;

//Loop to print the upper half

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

{

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

{

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

}

System.out.println();

}

Java Solutions Bank-Patterns 2010

11 TechSparx Java Tuitions - 9880 205065

//Loop to print the lower half

for(int i = n-1; i >= 1; i--)

{

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

{

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

}

System.out.println();

}

}

}

/*

Twenty First Pattern

*

* *

* * *

* * * *

* * * * *

* * * *

* * *

* *

*

Twenty Second Pattern

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

1 2 3 4

1 2 3

1 2

1

Twenty Third Pattern

1

2 2

3 3 3

4 4 4 4

5 5 5 5 5

4 4 4 4

3 3 3

2 2

1

*/

Java Solutions Bank-Patterns 2010

12 TechSparx Java Tuitions - 9880 205065

public class Pattern04

{

public static void main(String[] args)

{

System.out.println("Thirty First Pattern ");

printPattern31();

System.out.println("\nThirty Second Pattern ");

printPattern32();

System.out.println("\nThirty Third Pattern ");

printPattern33();

}

/*

*

* *

* * *

* * * *

* * * * *

*/

public static void printPattern31()

{

int n = 5;

int numOfSpaces = n-1;

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

{

//Loop to print the spaces

for(int j = 1; j <= numOfSpaces; j++)

{

System.out.print(" ");

}

numOfSpaces--;

//Loop to print the stars

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

{

System.out.print(" *"); //Note: Space before star

}

System.out.println();

}

}

Java Solutions Bank-Patterns 2010

13 TechSparx Java Tuitions - 9880 205065

/*

* * * * *

* * * *

* * *

* *

*

*/

public static void printPattern32()

{

int n = 5;

int numOfSpaces = 0;

for(int i = n; i >= 1; i--)

{

//Loop to print the spaces

for(int j = 1; j <= numOfSpaces; j++)

{

System.out.print(" ");

}

numOfSpaces++;

//Loop to print the stars

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

{

System.out.print(" *"); //Note: Space before star

}

System.out.println();

}

}

Java Solutions Bank-Patterns 2010

14 TechSparx Java Tuitions - 9880 205065

/*

*

* *

* * *

* * * *

* * * * *

* * * *

* * *

* *

*

*/

public static void printPattern33()

{

int n = 5;

int numOfSpaces = n-1;

//Loop to print the upper half of pattern

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

{

//Loop to print the spaces

for(int j = 1; j <= numOfSpaces; j++)

{

System.out.print(" ");

}

numOfSpaces--;

//Loop to print the stars

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

{

System.out.print(" *"); //Note: Space before star

}

System.out.println();

}

//Loop to print the lower half of pattern

numOfSpaces = 1;

for(int i = n - 1; i >= 1; i--)

{

//Loop to print the spaces

for(int j = 1; j <= numOfSpaces; j++)

{

System.out.print(" ");

}

numOfSpaces++;

//Loop to print the stars

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

{

System.out.print(" *"); //Note: Space before star

}

System.out.println();

}

}

}

Java Solutions Bank-Patterns 2010

15 TechSparx Java Tuitions - 9880 205065

/*

Thirty First Pattern

*

* *

* * *

* * * *

* * * * *

Thirty Second Pattern

* * * * *

* * * *

* * *

* *

*

Thirty Third Pattern

*

* *

* * *

* * * *

* * * * *

* * * *

* * *

* *

*

*/

Java Solutions Bank-Patterns 2010

16 TechSparx Java Tuitions - 9880 205065

public class Pattern05

{

public static void main(String[] args)

{

System.out.println("Forty First Pattern ");

printPattern41();

System.out.println("\nForty Second Pattern ");

printPattern42();

System.out.println("\nForty Third Pattern ");

printPattern43();

}

/*

*

***

*****

*******

*********

*/

public static void printPattern41()

{

int n = 5;

int numOfSpaces = n - 1;

int numOfStars = 1;

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

{

//print the spaces

for(int j = 1; j <= numOfSpaces; j++)

{

System.out.print(" ");

}

numOfSpaces--;

//print the stars

for(int j = 1; j <= numOfStars; j++)

{

System.out.print("*");

}

numOfStars += 2;

System.out.println();

}

}

Java Solutions Bank-Patterns 2010

17 TechSparx Java Tuitions - 9880 205065

/*

*********

*******

*****

***

*

*/

public static void printPattern42()

{

int n = 5;

int numOfSpaces = 0;

int numOfStars = 9;

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

{

//print the spaces

for(int j = 1; j <= numOfSpaces; j++)

{

System.out.print(" ");

}

numOfSpaces++;

//print the stars

for(int j = 1; j <= numOfStars; j++)

{

System.out.print("*");

}

numOfStars -= 2;

System.out.println();

}

}

Java Solutions Bank-Patterns 2010

18 TechSparx Java Tuitions - 9880 205065

/*

*

***

*****

*******

*********

*******

*****

***

*

*/

public static void printPattern43()

{

int n = 5;

int numOfSpaces = n - 1;

int numOfStars = 1;

//Loop to print the upper half of the stars

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

{

//print the spaces

for(int j = 1; j <= numOfSpaces; j++)

{

System.out.print(" ");

}

numOfSpaces--;

//print the stars

for(int j = 1; j <= numOfStars; j++)

{

System.out.print("*");

}

numOfStars += 2;

System.out.println();

}

numOfSpaces = 1;

numOfStars -=4;

//Loop to print the lower half of the stars

for(int i = 1; i <= n-1; i++)

{

//print the spaces

for(int j = 1; j <= numOfSpaces; j++)

{

System.out.print(" ");

}

numOfSpaces++;

//print the stars

for(int j = 1; j <= numOfStars; j++)

{

System.out.print("*");

}

numOfStars -= 2;

System.out.println();

}

}

}

Java Solutions Bank-Patterns 2010

19 TechSparx Java Tuitions - 9880 205065

/*

Forty First Pattern

*

***

*****

*******

*********

Forty Second Pattern

*********

*******

*****

***

*

Forty Third Pattern

*

***

*****

*******

*********

*******

*****

***

*

*/

Java Solutions Bank-Patterns 2010

20 TechSparx Java Tuitions - 9880 205065

public class Pattern06

{

public static void main(String[] args)

{

System.out.println("Fifty First Pattern ");

printPattern51();

System.out.println("\nFifty Second Pattern ");

printPattern52();

System.out.println("\nFifty Third Pattern ");

printPattern53();

}

/*

1

121

12321

1234321

123454321

*/

private static void printPattern51()

{

int n = 5;

int numOfSpaces = n - 1;

int middleNumber = 1;

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

{

//print the spaces

for(int j = 1; j <= numOfSpaces; j++)

{

System.out.print(" ");

}

numOfSpaces--;

//print the first half of the numbers of the current row

for(int j = 1; j <= middleNumber; j++)

{

System.out.print(j);

}

//print the second half of the numbers of the current row

for(int j = middleNumber-1 ; j >= 1; j--)

{

System.out.print(j);

}

middleNumber++;

System.out.println();

}

}

Java Solutions Bank-Patterns 2010

21 TechSparx Java Tuitions - 9880 205065

/*

123454321

1234321

12321

121

1

*/

private static void printPattern52()

{

int n = 5;

int numOfSpaces = 0;

int middleNumber = n;

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

{

//print the spaces

for(int j = 1; j <= numOfSpaces; j++)

{

System.out.print(" ");

}

numOfSpaces++;

//print the first half of the numbers of the current row

for(int j = 1; j <= middleNumber; j++)

{

System.out.print(j);

}

//print the second half of the numbers of the current row

for(int j = middleNumber-1 ; j >= 1; j--)

{

System.out.print(j);

}

middleNumber--;

System.out.println();

}

}

Java Solutions Bank-Patterns 2010

22 TechSparx Java Tuitions - 9880 205065

/*

1

121

12321

1234321

123454321

1234321

12321

121

1

*/

private static void printPattern53()

{

int n = 5;

int numOfSpaces = n - 1;

int middleNumber = 1;

//Loop to print the upper half of the pattern

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

{

//print the spaces

for(int j = 1; j <= numOfSpaces; j++)

{

System.out.print(" ");

}

numOfSpaces--;

//print the first half of the numbers of the current row

for(int j = 1; j <= middleNumber; j++)

{

System.out.print(j);

}

//print the second half of the numbers of the current row

for(int j = middleNumber-1 ; j >= 1; j--)

{

System.out.print(j);

}

middleNumber++;

System.out.println();

}

numOfSpaces = 1;

middleNumber = n-1;

//Loop to print the Lower half of the pattern

for(int i = 1; i <= n-1; i++)

{

//print the spaces

for(int j = 1; j <= numOfSpaces; j++)

{

System.out.print(" ");

}

numOfSpaces++;

//print the first half of the numbers of the current row

for(int j = 1; j <= middleNumber; j++)

{

System.out.print(j);

}

Java Solutions Bank-Patterns 2010

23 TechSparx Java Tuitions - 9880 205065

//print the second half of the numbers of the current row

for(int j = middleNumber-1 ; j >= 1; j--)

{

System.out.print(j);

}

middleNumber--;

System.out.println();

}

}

}

/*

Fifty First Pattern

1

121

12321

1234321

123454321

Fifty Second Pattern

123454321

1234321

12321

121

1

Fifty Third Pattern

1

121

12321

1234321

123454321

1234321

12321

121

1

*/

Java Solutions Bank-Patterns 2010

24 TechSparx Java Tuitions - 9880 205065

public class Pattern07

{

public static void main(String[] args)

{

System.out.println("Sixty First Pattern");

printPattern61();

}

/*

A

ABA

ABCBA

ABCDCBA

ABCDEDCBA

ABCDCBA

ABCBA

ABA

A

*/

private static void printPattern61()

{

int n = 5;

int numOfSpaces = n - 1;

char middleLetter = 'A';

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

{

//print the spaces

for(int j = 1; j <= numOfSpaces; j++)

{

System.out.print(" ");

}

numOfSpaces--;

//print the first half of the letters of the current row

for(char j = 'A'; j <= middleLetter; j++)

{

System.out.print(j);

}

//print the second half of the letters of the current row

for(char j = (char)(middleLetter - 1); j >= 'A'; j--)

{

System.out.print(j);

}

middleLetter++;

System.out.println();

}

//Loop to print the lower half of the pattern

n = n - 1;

numOfSpaces = 1;

middleLetter -= 2;

for(int i = n; i >= 1; i--)

{

//print the spaces

for(int j = 1; j <= numOfSpaces; j++)

Java Solutions Bank-Patterns 2010

25 TechSparx Java Tuitions - 9880 205065

{

System.out.print(" ");

}

numOfSpaces++;

//print the first half of the letters of the current row

for(char j = 'A'; j <= middleLetter; j++)

{

System.out.print(j);

}

//print the second half of the letters of the current row

for(char j = (char)(middleLetter - 1); j >= 'A'; j--)

{

System.out.print(j);

}

middleLetter--;

System.out.println();

}

}

}

/*

Sixty First Pattern

A

ABA

ABCBA

ABCDCBA

ABCDEDCBA

ABCDCBA

ABCBA

ABA

A

*/

Java Solutions Bank-Patterns 2010

26 TechSparx Java Tuitions - 9880 205065

I am Saravanan.G, TechSparx is my start-up.

I have completed Bachelor of Engineering in Computer Science and

working as a Senior Software Engineer in a MNC

My passion is teaching!

I love teaching programming languages like C, C++ and Java.

My motto is to provide Corporate Level Training to School and

Engineering College Students.

If you want to learn the ART of programming in C, C++ or JAVA easily!!!

Then feel free to call me@ 9880 205065 or mail me

[email protected]

I conduct classes in the following locations

Manipal institute of Computer Education

213/60, 11th Cross, Next to Canara Bank, Opp.

Brand Factory, Wilson Garden, Bangalore – 27

Vonov Technologies

Maratha Halli, Bangalore

For more information visit www.techsparx.webs.com