Java Pattern Programming
Java Pattern Programming
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
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
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
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
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
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
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
}
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
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
N=4 *******
*****
***
*
No. of Columns
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