0% found this document useful (0 votes)
24 views81 pages

Java Pattern Programming

java pattern programming

Uploaded by

rachitvaishnav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views81 pages

Java Pattern Programming

java pattern programming

Uploaded by

rachitvaishnav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 81

Printing * horizontally

Sample Sample Output:


Input: N = 4 ****
Pseudocode
Input: N

1 to ____
for starcount = ____ N
{
Print “*”
}
Code
import java.util.Scanner;
class Main{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
int N = in.nextInt();
for(int starcount = 1; starcount <= N; starcount++){
System.out.print("*”);
}
}
}
Printing numbers from 1 to N

Sample Sample Output:


Input: N = 4 1234
Pseudocode
Input: N

1 to ___
for num = ____ N
{
Print num
}
Code
import java.util.Scanner;
class Main{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
int N = in.nextInt();
for(int num = 1; num <= N; num++){
System.out.print(num);
}
}
}
Printing numbers from 1 to N with comma

Sample Sample Output:


Input: N = 4 1,2,3,4
Pseudocode
Input: N
Sample Input :
1 to ___
N 4
for num = ___
{ Actual Output :
Print num 1,2,3,4,
Print “,”
}
Pseudocode
Input: N

for num = ___ 1 to ___


N
{
Print num
If(num != N){
Print “,”
}
}
Code
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
for(int num = 1 ; num <= N ; num++) {
System.out.print(num);
Code
if(num != n){
System.out.print(",");
}
}
}
}
Printing * with #

Sample Sample Output:


Input: N = 8 ***#***#**
M=3
Sample Input :
Pseudocode N=9
M=3
Input: N, M
Actual Output :
1 to ___
for starcount = ___ N ***#***#***#
{
Print “*” Expected Output :
If(starcount % M == 0){ ***#***#***
Print “#”
}
}
Pseudocode
Input: N, M

1 to ___
for starcount = ___ N
{
Print “*”
If(starcount % M == 0 && starcount != N){
Print “#”
}
}
Code
import java.util.Scanner;
class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int M = in.nextInt();
for(int starcount = 1 ; starcount <= N ; starcount++) {
System.out.print("*");
Code
if((starcount % M == 0) && (starcount != N))
System.out.print("#");
}
}
}
Printing * in 2 – D

Sample Sample Output:


Input: N = 4 **
M=2 **
**
**
Pseudocode
Input: N, M Better Coding
convention…?
total_count = N * M

1 to ____________
for starcount = ____ total_count
{
Print “* ”
If(starcount % M == 0){
Print “\n”
}
}
Key Idea
For any value
Sample Input:
N=4 Row No No. of Columns Row No No. of Columns
M=2 (No. of Stars) (No. of Stars)

1 2 1 2
Sample Output:
** 2 2 2 2
** 3 2
…. ….
**
4 2 x 2
**
Pseudocode
Input: N, M

for row_no = ___1 to ___


N
{
for col_no = ___ 1to ___{M
Print “*”
}
Print “\n”
}
Code
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
Code
for(int row_no = 1; row_no<= n; row_no++) {
for(int col_no = 1; col_no <= m; col_no++)
{
System.out.print("*");
}
System.out.println();
}
}
}
Printing N lines of stars in Square fashion

Sample Sample Output:


Input: N = 4 ****
****
****
****
Key Idea
For any value
Sample Input:
N=4 Row No No. of Columns Row No No. of Columns
(No. of Stars) (No. of Stars)
Sample Output:
1 4 1 4
****
**** 2 4 2 4
**** 3 4
…. ….
****
4 4 x 4
Pseudocode
Input: N

for row_no = ___1 to ___


N
{
for col_no = ___ 1to ___{N
Print “*”
}
Print “\n”
}
Code
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
Code
for(int row_no = 1; row_no<= n; row_no++) {
for(int col_no = 1; col_no <= n; col_no++)
{
System.out.print("*");
}
System.out.println();
}
}
}
Printing stars in right angle triangle

Sample Sample Output:


Input: N = 4 *
**
***
****
Key Idea
For any value
Sample Input:
N=4 Row No No. of Columns Row No No. of Columns
(No. of Stars) (No. of Stars)
Sample Output:
1 1 1 1
*
** 2 2 2 2
*** 3 3
…. ….
****
4 4 x x
Pseudocode
Input: N

for row_no = ___1 to ___


N
{
for col_no = ___ 1to ________{
row_no
Print “*”
}
Print “\n”
}
Code
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
Code
for(int row_no = 1; row_no<= n; row_no++) {
for(int col_no = 1; col_no <= row_no; col_no++)
{
System.out.print("*");
}
System.out.println();
}
}
}
Printing numbers in right angle triangle

Sample Sample Output:


Input: N = 4 1
12
123
1234
Pseudocode
Input: N

1 to ___
for row_no = ___ N
{
for col_no = ___ 1to ________{
row_no
Print col_no
}
Print “\n”
}
Code
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
Code
for(int row_no = 1; row_no<= n; row_no++) {
for(int col_no = 1; col_no <= row_no; col_no++)
{
System.out.print( col_no);
}
System.out.println();
}
}
}
Printing increment numbers in right angle triangle

Sample Sample Output:


Input: N = 4 1
23
456
7 8 9 10
Pseudocode
Input: N
num = 1
1 to ___
for row_no = ___ N
{
for col_no = ___ 1to ________{
row_no
Print num
num++
}
Print “\n”
}
Code
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int num=1;
Code
for(int row_no = 1; row_no <= n; row_no++) {
for(int col_no = 1; col_no <= row_no ; col_no++) {
System.out.print(num);
num++;
}
System.out.println();
}
}
}
Printing * and # in right angle triangle

Sample Sample Output:


Input: N = 4 #
#*
#*#
#*#*
Pseudocode
Input: N
num = 1
for row_no = ___ 1 to ___
N
{
for col_no = ___ 1 row_no
to ________{
If(col_no % 2 == 0){
Print “*”
}
Else{
Print “#”
}
}
Print “\n”
}
Code
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
int n=s.nextInt();
for(int row_no=1; row_no<=n; row_no++) {
for(int col_no=1; col_no<=row_no; col_no++) {
if(col_no%2 == 0) {
System.out.print("* "); }
Code
else
{
System.out.print("# ");
}
}
System.out.println();
}
}
}
Printing numbers in inverted right angle triangle

Sample Sample Output:


Input: N = 4 1234
123
12
1
Pseudocode
Sample Input: Input: N
4 N
num = ___
1 to _______
for row_no = ____ N
Sample Output: {
1234 // Handle stars for each row
123 1 to ________
for col_no = ____ num
12 {
1 col_no
print ______________
}
print "\n"
num = num - 1;
}
Printing stars in pyramid

Sample Sample Output:


Input: N = 4 *
**
***
****
No. of Columns
Row No
Key Idea No. of Spaces No. of stars

1 3 1
Sample Input: 2 2 2
N=4
3 1 3
Sample Output: 4 0 4
* For any value
** No. of Columns
*** Row No
No. of Spaces No. of stars
****
1 N-1 1
2 N-2 2
…. …. ….
x N-x x
Pseudocode Input: N
for row_no = ___1 to ______
N
{
No. of Columns // Handle spaces for each row
Row No for space = ___ N - row_no
1 to ____________
No. of Spaces No. of stars
{
1 N-1 1 print " "
2 N-2 2 }
…. …. …. // Handle stars for each row
row_no
1 to _________
for col_no = ___
x N-x x {
print "* "
}
print "\n"
}
Printing stars in inverse pyramid

Sample Sample Output:


Input: N = 4 ****
***
**
*
No. of Columns
Row No
Key Idea No. of Spaces No. of stars

1 0 4
Sample Input: 2 1 3
N=4
3 2 2
Sample Output: 4 3 1
**** For any value, star_count = N
*** No. of Columns
** Row No
No. of Spaces No. of stars
*
1 1-1 4
2 2-1 3
…. …. ….
x x-1 star_count
Input: N

Pseudocode for row_no = ___ to ____


{
// Handle space for each row
For any value, star_count = N for space = ____ to ____________
No. of Columns {
Row No
No. of Spaces No. of stars print " “
}
1 1-1 4 // Handle stars for each row
2 2-1 3 for curr_col_no = ____ to ____________
…. …. …. {
x x-1 star_count print "* “
}
print "\n“

}
Code
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int star_count=n;
for(int row_no = 1; row_no <= n; row_no++){
for(int space = 1; space <= row_no - 1; space++){
System.out.print(" ");
}
Code
for(int curr_col_no = 1; curr_col_no <= star_count; curr_col_no)
{
System.out.print("* ");
}
System.out.println();
star_count = star_count-1;
}
}
}
Printing stars in diamond fashion

Sample Sample Output:


Input: N = 4 *
**
***
****
****
***
**
*
Pseudocode N
star_count = ___
for curr_row_no = ___1 to ____
N
Input: N {
for curr_row_no = ___1 to ______
N // Handle space for each row
curr_row_no-1
1 to _____________
{ for space = ____
// Handle spaces for each row {
for space = ___ N - curr_row_no
1 to ________________ print " "
{ }
print " " // Handle stars for each row
star_count
1 to ___________
} for curr_col_no = ____
// Handle stars for each row {
for curr_col_no = ___ curr_row_no
1 to ___________ print "* "
{ }
print "* " print "\n"
} star_count = star_count - 1
print "\n" }
}
Code
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
for(int curr_row_no = 1; curr_row_no <= n; ++curr_row_no) {
for(int space = 1; space <= n – curr_row_no; ++space) {
System.out.print(" ");
}
for(int curr_col_no = 1; curr_col_no <= curr_row_no; ++curr_col_no)
{
System.out.print("* ");
}
System.out.println();
}
Code
int star_count = n;
for(int curr_row_no = 1; curr_row_no <= n; curr_row_no = curr_row_no + 1)
{
for(int space = 1; space <= curr_row_no - 1; space = space + 1)
{
System.out.print(" ");
}
for(int curr_col_no = 1; curr_col_no <= star_count; curr_col_no = curr_col_no + 1)
{
System.out.print("* ");
}
System.out.println();
star_count = star_count-1;
}
}
}
Printing stars in mirrored right triangle

Sample Sample Output:


Input: N = 4 *
**
***
****
No. of Columns
Row No
Key Idea No. of Spaces No. of stars

1 3 1
Sample Input: 2 2 2
N=4
3 1 3
Sample Output: 4 0 4
* For any value,
** No. of Columns
*** Row No
No. of Spaces No. of stars
****
1 N-1 1
2 N-2 2
…. …. ….
x N-x x
Input: N

Pseudocode for row_no = ___ 1 to ____


N
{
// Handle spaces for each row
For any value, N - row_no
1 to ____________
for space = _____
No. of Columns {
Row No
No. of Spaces No. of stars print " "
}
1 N-1 1 // Handle stars for each row
2 N-2 2 for col_no = ___ row_no
1 to __________
…. …. …. {
x N-x x print "*"
}
print "\n"
}
Code
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
for(int row_no = 1; row_no <= n; ++row_no)
{
for(int space = 1; space <= n - row_no; ++space)
{
System.out.print(" ");
}
for(int col_no = 1; col_no <= row_no; ++col_no)
{
System.out.print("*");
}
Code
System.out.println();
}
}
}
Printing stars in Inverse Half Diamond Pattern

Sample Input: Sample Output:

N=4 *******
*****
***
*
No. of Columns

Key Idea Row No No. of Spaces No. of stars


1 0 7
2 1 5
Sample Input: 3 2 3
N=4 4 3 1
For any value, star_count = (2*N)-1
Sample Output: No. of Columns
Row No No. of Spaces No. of stars
******* 1 0 star_count
(2*N)-1
***** 2 1 star_count - 2
*** …. …. ….
* x x-1 star_count – 2
Pseudocode for row_no = ___
{
N
1 to ______

// Handle spaces for each row


1 to ___________
for space = ___ row_no - 1
For any N value, star_count = (2*N)-1 {
No. of Columns print " “
Row No }
No. of Spaces No. of stars // Handle stars for each row
1 0 star_count for col_no = ___ star_count - 2*(row_no – 1)
1 to ___________________________
2 1 star_count - 2 {
print "*“
…. …. …. }
Anything
x x-1 star_count – 2 print "\n“ Missing?
star_count = star_count – 2
}
Input: N
2*N-1
star_count = ____________
Alternate way ?
Code
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int starcount = 2 * n - 1;
for(int row_no = 1; row_no <= n; ++row_no)
{
for(int space = 1; space <= row_no - 1; ++space)
{
System.out.print(" ");
}
Code
for(int col_no = 1; col_no <= starcount - (2 * (row_no - 1)) ; ++col_no)
{
System.out.print("*");
}
System.out.println();
}
}
}
Printing stars in Diamond pattern

Sample Input: Sample Output:

N=4 *
***
*****
*******
*****
***
*
Key Idea

* *
*** ***
***** ***** Half diamond pattern (N)
******* *******
*****
*** *****
Inverse half diamond pattern
* ***
(N - 1)
*
Pseudocode
for row_no = ___1 to ______
N
Input: N
for row_no = ___1 to ______
N {
{ // Handle spaces for each row
row_no
1 to ________
for space = ___
// Handle spaces for each row
1 to ____________
for space = ___ N - row_no {
{ print " "
print " " }
} // Handle stars for each row
1
for col_no = ___to star_count
____________
// Handle stars for each row
2 * row_no - 1
1 to __________________
for col_no = __ {
{ print "*"
print "*" }
} print "\n"
print "\n" star_count = star_count - 2
} }
(2*N-1)-1
star_count = ______________
Diamond pattern – Nos. and Stars

Sample Sample Output:


Input: N = 4 1
2*2
3*3*3
4*4*4*4
3*3*3
2*2
1
Pseudocode else
{
print "*“
Input: N
}
for row_no = 1 to N
}
{
print "\n"
// Handle spaces for each row
}
for space = 1 to N – row_no
{
print " "
}
1
// Handle stars for each row 2*2
for col_no = 1 to 2 * row_no - 1 3*3*3
{ 4*4*4*4
col_no % 2 == 1
if(_________________________) 3*3*3
{ 2*2
row_no 1
print ______________
}
Pseudocode Cont..
num = N -1
}
N--
else
no_star_count = (2*N) - 1
{
for row_no = 1 to N
print “*“
{
}
// Handle spaces for each row
}
for space = 1 to row_no -1
print "\n"
{
no_star_count = no_star_count – 2
print " "
num = num -1
}
} 1
// Handle stars for each row 2*2
for col_no = 1 to no_star_count 3*3*3
{ 4*4*4*4
col_no % 2 == 1
if(________________) 3*3*3
{ 2*2
num 1
print _______
Triangle pattern – Nos. and Stars

Sample Sample Output:


Input: 1
N=4
2*2
3*3*3
4*4*4*4
4*4*4*4
3*3*3
2*2
1
Pseudocode
Input: N else
for row_no = 1 to N {
{ print "*“
// Handle spaces for each row }
for space = 1 to N – row_no }
{ print "\n“
print “ “ }
}
// Handle stars for each row 1
for col_no = 1 to 2 * row_no – 1 1 2*2
{ 2*2 3*3*3
col_no % 2 == 1
if(_________________) 3*3*3 4*4*4*4
4*4*4*4 4*4*4*4
{ 3*3*3 3*3*3
row_no
print ________ 2*2 2*2
} 1 1
Pseudocode Contd…
num = N -1
else
N--
{
no_star_count = (2*N) - 1
print “*“
for row_no = 1 to N
}
{
}
// Handle spaces for each row
print "\n"
for space = 1 to row_no -1
star_count = no_star_count – 2
{
num = num - 1
print " "
} 1
}
1 2*2
// Handle stars for each row 2*2 3*3*3
for col_no = 1 to no_star_count 3*3*3 4*4*4*4
{ 4*4*4*4 4*4*4*4
col_no % 2 == 1
if(________________) 3*3*3
3*3*3
2*2
{ 1 2*2
num
print _____ 1
}
Printing custom number pattern

Sample Sample Output:


Input: 1112
N=4
3222
3334
5444
Key Idea row_no = even
1112
3222
row_no = odd
and
and col_no = N
Input: N 3334
col_no = 1
num = 1 5444
for row_no = 1 to N
{
for col_no = 1 to N{
Print ifrow_no
((row_no % 2 == 0) && (col_no == 1)) ||
(_____________________________________
} ((row_no % 2 == 1) && (col_no == N))
_____________________________________) 1111
Print {“\n” 2222
} print row_no + 1
} 3333
else 4444
{
print row_no
}
THANK YOU

You might also like