Java print a pattern with nested for loop - java

I am struggling to build an algorithm that would print the much needed pattern.
The code is the following:
public static void printPatternH(int size)
{
for (int row = 1; row <= size; row++)
{
for (int col = 1; col <= 2*size; col++)
{
if (col > size + row - 1) {
continue;
}
if (col <= size) {
System.out.print((row + col >= size + 1 ? (row + col)%size : " ") + " ");
}
else {
System.out.print((row + col >= size + 1 ? (row + size)%col : " ") + " ");
}
}
System.out.println();
}
}
The result is:
I understand that if size is 9 the last number in the middle will be 0 as (row + size)%col = 0, however I couldn't figure out a way to modify it without changing the rest of the values.

Change
(row + col)%size
to
(row + col - 1) % size + 1

You can check for the "0" and replace it before printing it out:
if (col <= size) {
//print left hand side
int remainder = (row + col) % size;
if (remainder == 0) remainder = size; //replace the "0" with size here.
System.out.print((row + col >= size + 1 ? remainder : " ") + " ");
} else {
//print right hand side
System.out.print((row + col >= size + 1 ? (row + size) % col : " ") + " ");
}
It will give this output:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 6 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1

public class Testb
{
int item = 9;
int limit = 5;
int half = item/2;
public static void main(String[] args)
{
//System.out.println("Hello World!");
for(int i=0;i<limit;i++){
for(int j=0;j<half;j++){
if(j<(half-i)){
print(" ");
}else{
print(j-(half-i))
}
}
print(i);
for(int k=half;k<(half+i);k++){
print((half+i)-(k+1));
}
println();
}
}
}
output:
0
0 1 0
0 1 2 1 0
0 1 2 3 2 1 0
0 1 2 3 4 3 2 1 0

int rowCount = 1;
System.out.println("Here Is Your Pyramid");
//Implementing the logic
for (int i = noOfRows; i > 0; i--)
{
//Printing i*2 spaces at the beginning of each row
for (int j = 1; j <= i*2; j++)
{
System.out.print(" ");
}
//Printing j value where j value will be from 1 to rowCount
for (int j = 1; j <= rowCount; j++)
{
System.out.print(j+" ");
}
//Printing j value where j value will be from rowCount-1 to 1
for (int j = rowCount-1; j >= 1; j--)
{
System.out.print(j+" ");
}
System.out.println();
//Incrementing the rowCount
rowCount++;
}

Related

Magic Square program Java with array

I want to program a Magic Square wherein the utilization of arrays is at place but when i want to run it, it shows Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at MagicSquare.main(MagicSquare.java:6)
What should I do? It would show
4 9 2 7 1 6
3 5 7 not 3 5 7
8 1 6 4 9 2
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
int n = Integer.parseInt(args[3]);
if (n % 2 == 0) throw new RuntimeException("n must be odd");
int[][] magic = new int[n][n];
int row = n-1;
int col = n/2;
magic[row][col] = 1;
for (int i = 2; i <= n*n; i++) {
if (magic[(row + 1) % n][(col + 1) % n] == 0) {
row = (row + 1) % n;
col = (col + 1) % n;
}
else {
row = (row - 1 + n) % n;
}
magic[row][col] = i;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (magic[i][j] < 10) System.out.print(" ");
if (magic[i][j] < 100) System.out.print(" ");
System.out.print(magic[i][j] + " ");
}
System.out.println();
}
}
What else should I add or take away from this program?
According to your program, need to provide 4 arguments when running. ex: If class name is Test:
java Test 1 1 1 3
Result:
4 9 2
3 5 7
8 1 6

Cannot print pyramid of numbers

for (int i = 1; i <= 4; i++)
{
int n = 4;
for (int j = 1; j <= n - i; j++)
{
System.out.print(" ");
}
for (int k = i; k >= 1; k--)
{
System.out.print(k);
}
for (int l = 2; l <= i; l++)
{
System.out.print(l);
}
System.out.println();
}
for (int i = 3; i >= 1; i--)
{
int n = 3;
for (int j = 0; j <= n - i; j++)
{
System.out.print(" ");
}
for (int k = i; k >= 1; k--)
{
System.out.print(k);
}
for (int l = 2; l <= i; l++)
{
System.out.print(l);
}
System.out.println();
}
My output:
Enter height:
12
1
212
32123
4321234
543212345
65432123456
7654321234567
876543212345678
98765432123456789
109876543212345678910
1110987654321234567891011
12111098765432123456789101112
It prints correctly, but with no spacing...
Would I need another for loop with a println that just prints a space under the first for loop?
Also, if I did that would it still work for double digit heights such as 12?
I would greatly appreciate help, thank you.
You just need to play around with spaces.
for (int i = 1; i <= height; i++) {
int n = height;
int n2 = height + 1;
for (int j = 1; j <= n - i; j++) {
System.out.print("" + String.format("%3s", " ") + " ");
}
for (int k = i; k >= 1; k--) {
System.out.print("" + String.format("%3s", k) + " ");
}
for (int l = 2; l <= i; l++) {
System.out.print("" + String.format("%3s", l) + " ");
}
System.out.println();
System.out.println();
}
OUTPUT: For height 10
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7
8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9
10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10

Display a box of increasing number with order

CODE
int rows=3, columns=3, i, j;
for(i = 1; i <= rows; i++)
{
for(j = 1; j <= columns; j++)
{
if(i == 1 || i == rows || j == 1 || j == columns)
{
System.out.print(count);
count++;
}
else
{
System.out.print(" ");
}
}
System.out.print("\n");
}
The following piece of code outputs the following:
OUTPUT
123
4 5
678
What I'm trying to achieve is the following:
123
8 4
765
Basically creating a board game which has to start from one position and end at the same position completing a full circle., in this case a square.
Any Ideas ??
After some experiments, this could work:
public static void printBox(int rows, int columns) {
int sumOfColumns = 2 * columns + rows - 1;
int sumOfRow = 2 * columns + 2 * rows - 2;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= columns; j++) {
if (i == 1) {
System.out.printf(String.format("%3d", j));
} else if (j == 1) {
System.out.printf(String.format("%3d", sumOfRow - (i + j - 1)));
} else if (j == columns) {
System.out.printf(String.format("%3d", i + j - 1));
} else if (i == rows) {
System.out.printf(String.format("%3d", sumOfColumns - j));
} else {
System.out.print(" ");
}
}
System.out.print("\n");
}
System.out.print("\n");
}
Test case:
public static void main(String[] args) {
printBox(3, 3);
printBox(4, 4);
printBox(3, 4);
}
Result:
1 2 3
8 4
7 6 5
1 2 3 4
12 5
11 6
10 9 8 7
1 2 3 4
10 5
9 8 7 6
Consider creating a 2 dimensional array:
gameBoard[3][3];
then populate that board with the desired values. After the gameboard is complete, you can write another function to print the board.

My nested for loop number pattern doesn't out put the correct pattern

I'm trying to print out this pattern but I'm having trouble arranging the numbers and spacing them out to achieve the expected out put.
Expected Output:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Here is my Code. I don't know how to space the numbers and put the correct numbers in the order that the out put has specified.
import java.util.Scanner;
public class patterns{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
for (int i = 6; i >= 1; i--)
{
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
for (int j = i; j <= 6; j++)
{
System.out.print(j);
}
System.out.println();
}
System.out.println();
for (int i = 1; i <= 6; i++)
{
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
for (int j = i; j <= 6; j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
This is the Output I'm Getting
run:
6
56
456
3456
23456
123456
123456
23456
3456
456
56
6
You should think that each triangle block actually has 6x6 square cells. So it means you need 2 nested loops, each will iterate 6 times.
Then inside you will decide whether you print a number or space, depending on some condition.
for (int i = 6; i >= 1; i--)
{
for (int j = 1; j <= 6; j++)
{
if (j < i)
{
System.out.print(" ");
} else
{
System.out.print(7 - j + " ");
}
}
System.out.println("");
}
System.out.println("");
for (int i = 1; i <= 6; i++)
{
for (int j = 1; j <= 6; j++)
{
if (j < i)
{
System.out.print(" ");
} else
{
System.out.print(j-i+1 + " ");
}
}
System.out.println("");
}
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
int a = 654321;
String str = new Integer(a).toString();
for (int i = str.length() - 1; i >= 0 ; i--) {
String g = String.format("%6s", str.substring(i));
System.out.println(g.replaceAll(".(?=.)", "$0 "));
}
System.out.println();
str = new StringBuffer(str).reverse().toString();
for (int i = str.length() - 1; i >= 0 ; i--) {
String g = String.format("%6s", str.substring(0, i+1));
System.out.println(g.replaceAll(".(?=.)", "$0 "));
}
Here's the modified code for your patterns.
public class Patterns {
public static void main(String[] args) {
for (int i = 0; i < 6; i++) {
for (int j = 0; j < (5 - i) * 2; j++) {
System.out.print(" ");
}
for (int j = i + 1; j >= 1; j--) {
System.out.print(j + " ");
}
System.out.println();
}
System.out.println();
for (int i = 0; i < 6; i++) {
for (int j = 0; j < i * 2; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 6 - i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
Output
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Update: fixed bug for spaces
Working=> Consider the first half of the pattern
1 line 0: 10 spaces = (5 - 0) * 2 spaces
2 1 line 1: 8 spaces = (5 - 1) * 2 spaces
3 2 1 line 2: 6 spaces = (5 - 2) * 2 spaces
4 3 2 1 line 3: 4 spaces = (5 - 3) * 2 spaces
5 4 3 2 1 line 4: 2 spaces = (5 - 4) * 2 spaces
6 5 4 3 2 1 line 5: 0 spaces = (5 - 5) * 2 spaces
Consider the second half of the pattern
1 2 3 4 5 6 line 0: 0 spaces = 0 * 2 spaces
1 2 3 4 5 line 1: 2 spaces = 1 * 2 spaces
1 2 3 4 line 2: 4 spaces = 2 * 2 spaces
1 2 3 line 3: 6 spaces = 3 * 2 spaces
1 2 line 4: 8 spaces = 4 * 2 spaces
1 line 5: 10 spaces = 5 * 2 spaces
One way to do this would be to do:
import java.util.Scanner;
public class pattern{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
for (int i = 1; i <= 6; i++)
{
String val = "";
for (int j = i; j >= 1; j--)
{
val += j+ " ";
}
System.out.printf("%12s", val);
System.out.println();
}
System.out.println();
for (int i = 6; i >= 1; i--)
{
String val = "";
int ii=1;
for (int j = i; j >= 1; j--)
{
val += ii + " ";
ii++;
}
System.out.printf("%12s", val);
System.out.println();
}
}
Output:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
One thing to keep in mind is that when you are trying to format text like you are, you should use printf rather than adding print lines.
Best way is to use an array -> you loop less times. http://ideone.com/diYtvM Cheers!
import java.util.Scanner;
public class patterns{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int[][] arr = new int[6][6];
for(int i = 0; i<6; i++)
arr[i][5] = 1;
for(int i = 1; i<6; i++)
for (int j = 4; j>=5-i; j--)
{
arr[i][j] = arr[i-1][j+1] + 1;
}
for(int i = 0; i<6; i++)
{
for (int j = 0; j<6; j++)
{
if(arr[i][j] > 0)
System.out.print(arr[i][j] + " ");
else
System.out.print(" ");
}
System.out.println("");
}
System.out.println("");
int z = 0;
for(int i = 5; i>=0; i--)
{
for(int k = 0; k<z; k++)
System.out.print(" ");
for (int j = 5; j>=0; j--)
{
if(arr[i][j] > 0)
System.out.print(arr[i][j] + " ");
}
System.out.println("");
z++;
}
}
}
OUT:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

Can someone explain how Java nested loops work with number patterns?

I am having trouble understanding how nested loops and numbers patterns work. I have completed all of the patterns except 3. Can someone please help me with this code and explain how this works?
public class Patterns7{
public Patterns7() {
}
public void displayPatternI(int lines)
{
System.out.println("\n\tPattern I\n");
for (int i = 1; i <= lines; i++)
{
for (int j = 1; j <= i; j++)
System.out.print (j + " " );
System.out.println();
}
}
public void displayPatternII (int lines)
{
System.out.println("\n\tPattern II to be implemented\n");
for (int i = 1;i <= lines; i++)
{
for(int j = i;j >= 1; j--)
System.out.print(j);
System.out.println();
}
System.out.println();
}
public void displayPatternIII (int lines)
{
System.out.println("\n\tPattern III to be implemented\n");
for (int i = 1; i <= lines; i++)
{
for (int space = 1; space <= lines-i; space++)
System.out.print (" ");
for (int j = 1; j <= i; j++)
System.out.print (j + " ");
System.out.println();
}
System.out.println();
}
}
Pattern III is supposed to look like this:
6
56
456
3456
23456
123456
But all I could get it to do was this:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
I am unsure of how to get the out put to start with 6 and decrease then increase.
Pattern V is supposed to look like this:
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
But it comes out like this:
1
1 2 3
1 2 3 4 5
Code:
public void displayPatternVI (int lines)
{
System.out.println("\n\tMy Own Pattern to be implemented\n");
for (int i = 1; i <= lines/2; i++)
{
for (int space = 1; space <= (lines/2)-i; space++)
System.out.print (" ");
for (int j = 1; j <= (i*2)-1; j++)
System.out.print (j + " ");
System.out.println();
}
for (int i = 1; i <= lines/2; i++)
{
for (int space = 1; space <= i-1; space++)
System.out.print (" ");
for (int j = 1; j <= lines-(i*2)+1; j++)
System.out.print (j + " ");
System.out.println();
}
System.out.println();
}
Pattern VI is supposed to look like this:
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
6 5 4 3 2 1 2 3 4 5 6
5 4 3 2 1 2 3 4 5
4 3 2 1 2 3 4
3 2 1 2 3
2 1 2
1
but it looks like this:
1
1 2 3
1 2 3 4 5
1 2 3 4 5
1 2 3
1
Can someone please help and explain to me how to this?
You're really only supposed to ask one question at a time, but here's a solution to your first issue. For SIZE equals 6:
6
56
456
3456
23456
123456
you would want something like:
String temp;
for(int i = 0; i < SIZE; i++)
{
temp = "";
for(int j = SIZE - i; j <= SIZE ; j++)
{
temp += j;
}
System.out.printf("%" + SIZE + "s\n", temp);
}
Try the second one on your own.
public void displayPatternIII (int lines)
{
System.out.println("\n\tPattern III to be implemented\n");
for (int i = lines; i >= 1; i--)
{
int space = 1;
for (; space <= i-1; space++)
System.out.print (" ");
for (int j = space; j <= lines; j++)
System.out.print (j);
System.out.println();
}
}
Output:
Pattern III to be implemented
6
56
456
3456
23456
123456

Categories

Resources