how to make following pattern in java using nested for loop
1
21
321
4321
54321
654321
I was trying it with this one.
for (int r = 1; r <= 6; r++) {
for (int s = 6; s > row; s--) {
System.out.print(" ");
}
for (int c = 1; c<=r; c++) {
System.out.print(c);
}
System.out.println("");
}
but output of this code was:
1
12
123
1234
12345
123456
expected result:
1
21
321
4321
54321
654321
If you want to start at 6 and go down, then you need to start at row in this loop:
for (int col = 1; col<=row; col++) {
System.out.print(col);
}
Change it to:
for (int col = row; col > 0; col--) {
System.out.print(col);
}
Only two nested loops are enough to achieve that pattern you asked for
for (int row = 1; row < 7; row++) {// for loop for rows
for (int spaces = 6; spaces > 0; spaces--) {
if(spaces> row)
System.out.print(" ");
else
System.out.print(spaces);
}
System.out.println("");
}
Related
How to print a pattern where i and j variable are not supposed to be printed for pattern rows? I try many times I change values without results.
int k = 1, p = 0;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
p += k;
System.out.print(p + " ");
}
k++;
System.out.println();
}
The above code is the generated output:
1
3 5
8 11 14
18 22 26 30
35 40 45 50 55
The desired output:
1
3 5
5 8 11
7 11 15 19
9 14 19 24 29
p must be updated after each i loop, using the current value of k. The following output can be generated using this code:
int k = 0;
for (int i = 1; i <= 5; i++) {
int p = k++;
for (int j = 1; j <= i; j++) {
p += k;
System.out.print(p + " ");
}
System.out.println();
}
For better formatting, you can use
System.out.printf("%-4d", p);
I'd write it this way:
int numRows = 5;
for (int row = 1; row <= numRows; row++) {
for (int col = 1, p = (row*2)-1; col <= row; col++, p+=row) {
System.out.print(p + " ");
}
System.out.println();
}
The pattern I see is as follows:
The first Row is designated as Row #1 (not zero)
The starting number in each row is (Row * 2) - 1
Each Row has the same number of Columns as the current Row number
In each Row, the numbers are incremented by the current Row number
I'm trying to create a number grid in Java that would look like this:
789
456
132
public class Test {
public static void main(String[] args) {
//create grid
int [] [] grid = new int [3][3];
grid [0][0] = 7;
grid [1][0] = 8;
grid [2][0] = 9;
grid [0][1] = 4;
grid [1][1] = 5;
grid [2][1] = 6;
grid [0][2] = 1;
grid [1][2] = 2;
grid [2][2] = 3;
int rows = 3;
int columns = 3;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.println(grid[i][j] + " ");
}
System.out.println("");
}
}
}
the result that I'm getting is:
7
4
1
8
5
2
9
6
3
Any suggestions on to where I am going wrong? Many thanks in advance
System.out.println() prints a newline every time. Instead use System.out.print() in the inner loop. Also you need to switch i and j when printing:
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(grid[j][i] + " ");
}
System.out.println("");
}
use System.out.print(grid[j][i] + " "); you need to swap the position ofi and j because your numbers entered are in columns whereas you want them to be printed in rows.
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(grid[j][i] + " ");
}
System.out.println("");
}
output
7 8 9
4 5 6
1 2 3
I want to print asterix which is equals the number of rows - (they should begin from the right side). It should begin with 9 spaces and 1 * at the first line, then 2 * and 8 spaces at the second line and so on until the tenth row which should have 10 * and 0 spaces like the image below:
I can print the right number of spaces at each line and 1 * at the right position. For example it prints 9 spaces and 1 * at the first line. But then at the second line it prints 8 spaces and 1 * at the ninth position instead of two?? I can't see what I have missed in my code:
for (int row = 1; row <= 10; row++) {
for (int col = 10; col > row; col--) {
System.out.print(" ");
if (col <= row) {
System.out.print("*");
}
}
System.out.println("*");
}
There are several solutions:
1) Either you create two inner loops for each row: one to write the spaces and another to write the stars
final int rows = 10;
for(int row = 1; row <= rows; row++) {
for(int i = 0; i < (rows - row); i++) {
System.out.print(" ");
}
for(int i = 0; i < (row); i++) {
System.out.print("*");
}
System.out.println();
}
2) Or you create one inner loops for each row and check the index to consider if you have to print a star or a blank.
final int rows = 10;
for(int row = 1; row <= rows; row++) {
for(int col = 1; col <= rows; col++) {
System.out.print((col <=(rows - row))? " " : "*");
}
System.out.println();
}
3) Or you can use string manipulation with subString (this is ugly but why not):
final int rows = 10;
final String stars = "************************";
final String blanks = " ";
for(int row = 1; row <= rows; row++) {
System.out.print(blanks.substring(0, rows - row));
System.out.println(stars.substring(0, row));
}
This might also be helpful:
int n = 10;
int k = 2 * n - 2; // number of spaces
// outer loop to handle number of rows
for (int i = 1; i <= n; i++) {
// inner loop to handle number spaces
for (int j = 1; j <= k; j++) {
System.out.print(" "); // printing spaces
}
k = k - 2; // decrementing k after each loop
// inner loop to handle number of columns
for (int j = 1; j <= i; j++) {
System.out.print("* "); // printing stars
}
// ending line after each row
System.out.println();
}
My apologies if this individual question is a duplicate, but I have not seen any specific answers to this particular problem.
I am trying to put spaces in between each individual character in a triangle I created using nested for loops in the code below.
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= 1; j--) {
if (j > i)
System.out.print(" ");
else
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 = 1; j <= (5 - i + 1); j++) {
System.out.print(j);
}
System.out.println();
}
This outputs:
1
21
321
4321
54321
12345
1234
123
12
1
However, it is required to be:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
I've tried putting " " on either as well as both sides of the print statement, as I did with the reverse of these two triangles, to no avail. Am I missing something?
You can also try something like this.
int spaces=10;
int z;
for (int i = 1; i <=5 ; i++) {
spaces=spaces-2;
for (int j = 0; j<=spaces ; j++) {
if(j!=spaces)
System.out.print(" ");
else {
z=i;
while (z >= 1) {
System.out.print(z + " ");
z--;
}
}
}
System.out.println("\n");
}
System.out.println("\n");
spaces=-2;
int m;
for (int i = 1; i <=5 ; i++) {
spaces=spaces+2;
for (int j = 0; j <=spaces ; j++) {
if(j!=spaces)
System.out.print(" ");
else{
z=i;
m=1;
while (m<=6-z) {
System.out.print(m + " ");
m++;
}
}
}
System.out.println("\n");
}
Just change System.out.print(j); to System.out.print(j + " ");
I need to produce a triangle like:
6
55
444
3333
22222
111111
This is my code:
for (int row = 1; row <= size+1; row++) {
for (int col = 1; col <= size+1 - row ; col++)
{
System.out.print(row);
}
System.out.println();
}
which produces an upside down triangle.
This produces the desired results:
for (int row = 1; row <= size; row++) {
for (int col = 1; col <= row; col++) {
System.out.print((size - row + 1) + "\t");
}
System.out.println();
}
You'll need to remove the tab character though to get the results posted above.
This will help
int num=size;
for(int i=1;i<=size;i++){
for(int j=1;j<=i;j++){
System.out.print(num);
}
System.out.println();
num--;
}