Making a pattern with java using nested loops - java

I have to make a pattern that is of a diamond shape such as this:
1
1 2
1 2 3
1 2 3 4 5
1 2 3
1 2
1
I wrote this ( sorry if for the variable name, this is just a small part of my program and I'm trying to get the pattern right so I didn't mind my variables at the moment):
public class NewFile{
public static void main(String []args){
int k = 0;
for (int i=1 ; i<=5 ; i++)
{
{ for (int h=2 ; h >= i ; h--)
System.out.print(" ");
for (int j=1 ; j<= i + k ; j++)
System.out.print(j);
for (int w=2 ; w>= i; w--)
System.out.print(" ");
}
k++;
System.out.println();}
}
}
My output is the following:
1
123
12345
1234567
123456789
I realize I should divide the code into a lower and upper triangle using two loops. However, I don't know how to break the first part. I did find the "trend" but I don't see how to implement it.

The following code will display a diamond shape that is made up of asterisks:
int i = 0, j, k, n;
n = 7; // 7 characters high. Change as needed.
for (k = 1; k <= (n + 1) / 2; k++) {
for (i = 0; i < n - k; i++) {
System.out.print(" ");
}
for (j = 0; j < k; j++) {
System.out.print("* ");
}
System.out.println("");
}
for (k = ((n + 1) / 2); k < n; k++) {
for (i = 1; i < k; i++) {
System.out.print(" ");
}
for (j = 0; j < n - k; j++) {
System.out.print(" *");
}
System.out.println("");
}
The integer n represents the height of the diamond in characters and can be changed as needed.

Related

Mirrored Triangle

I am required to display a mirrored triangle like so:
0 1 2
0 1
0
But I am only able to get
0 1 2 3
1 2 3
2 3
3
I am unsure of what I am doing wrong and everything I've looked at only shows the star pattern, no number patterns. here is my code.
for (int i = 0; i <= size; i ++) {
for(int j = i; j <= size; j++) {
System.out.print(j + " ");
}
System.out.println(" ");
}
For each line, you have to print the leading spaces before printing the numbers. So you need two inner for loops, one for the spaces and one for the numbers:
for(int i = 0; i <= size; i++) {
// first print out the leading spaces
for(int j = 0; j < i; j++) {
System.out.print(" ");
}
// then print out the numbers
for(int j = 0; j <= size - i; j++) {
System.out.print(j + " ");
}
System.out.println("");
}

How do I put spaces in between individual characters when creating the triangle using nested for loops?

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 + " ");

Printing rows of increasing amounts of even numbers

I'm trying to print the following:
2
2 4
2 4 6 ..etc
The code I have written (below) prints the following:
2
4 6
8 10 12 ...etc
Can anyone spot where I'm going wrong? The n variable comes from the main method which I am not including.
public static void printEvenTable(int n) {
int i;
int j;
int k = 0;
for (i = 1; i <= n; i++) {
for (j = 0; j < i; j++)
System.out.print(" " + (k += 2));
System.out.println(" ");
}
}
You need to prevent the variable k from using its old value by reassigning 0 to it right after the second for loop. Placing k = 0; before the second for loop makes redundant reassigning because it has already been assigned right before the loop. Ensure program optimization. If you are using a good editor, it show you some warning if placed before the second for loop.
for (i = 1; i <= n; i++) {
for (j = 0; j < i; j++) {
System.out.print(" " + (k += 2));
}
k=0;
System.out.println(" ");
}
Here, the inner loop is increasing the variable k by 2 in each steps after the first execution of the inner loop k becomes 2 from the initial value 0. In the second iteration of the outer loop k starts as 2. After k+=2, k becomes 4 so second line of output starts from 4. That's why we need to re-initialize k to 0 before each inner loop.
public static void printEvenTable(int n) {
int i;
int j;
int k = 0;
for (i = 1; i <= n; i++) {
k = 0;
for (j = 0; j < i; j++)
System.out.print(" " + (k += 2));
System.out.println(" ");
}
}

Printing diamonds from *'s

I am trying to System.out.print() a diamond out of *'s. So far I have spent a good 5 hours on trying to figure out how to reverse print the bottom triangle of the diamond.
I can worry about the spacing to complete the diamond later. (I have it worked for the most part).
If someone could explain to me what I am doing wrong and how the right way works I would greatly appreciate it.
private static void diamond()
{
int numLines = 0;
System.out.println("How many lines would you like in the Diamond?");
numLines = scan.nextInt();
if (numLines / 2 == 0) //if number is even, make odd.
{
numLines++;
}
for(int i = 0; i <= numLines ; i++) // Controls #Lines
{
if(i <= numLines / 2)
{
for(int j = 0; j < i * 2 - 1; j++) // Controls #Stars small upright triangle
{
System.out.print("*");
}
}
else
{
for(int k = numLines; k > i / 2; k--) // Controls # of spaces
{
System.out.print("*");
}
/*for(int j = numLines/2 - i, l = i; l > j; j++) // Controls #Stars small upright triangle
{
String stars = "*";
System.out.print(stars);
}*/
}
System.out.println("");
}
}
`
What happens to your attempt is that you loop through the half (of the lines) of your diamond [the number of lines in second/first half] times.
You'd want to do a if-statement each loop, not an if and a for in each loop
Probably you want this
Just adjust it for user input values
public static void main(String[] args) {
System.out.print("Reverse diamond: \n");
for (int i = 1; i < 10; i += 2) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.print("\n");
}
System.out.print("\n\nDiamond from starts: \n");
for (int i = 7; i > 0; i -= 2) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.print("\n");
}
}
output:

How to print 1 to 10 with following format first line 1 2nd line 2 & 3 3rd line 4,5 & 6 4th line 7,8,9,10?

I tried like this,
for (int j = 1; j <= 4; j++) {
for (int k = 1; k < 10; k++) {
if (k <= j) {
System.out.print(k + " ");
} else
break;
}
System.out.println();
}
but it prints like
1
1 2
1 2 3
1 2 3 4
This question is asked in one interview i had attended, my mind is breaking to find solution.. i can't think..
init a variable before
int printed = 1;
and change your
System.out.print(k+" ");
with
System.out.print(printed+" ");
printed++;
This will do it :
public static void main(String[] args)
{
int i, j, k;
for (i = 1, j = 1; i < 11; j++)
{
for (k = 0; k < j; k++)
{
System.out.print(i++ + " ");
}
System.out.println();
}
}
This code will do that:
int i, j, k;
for (i = 1, j = 1; i < 11; j++) {
for (k = 0; k < j; k++)
System.out.print(i++ + " ");
System.out.print("\n");
}
Basically, i controls the next number to print, and j controls how many numbers you will print in the current line. k is just a counter for j.
You can also try this with some mathematic magic (sequences).
for (int j = 1; j <= 4; j++) {
for (int k=(int) (0.5*(Math.pow(j,2)-j+2));k<=(int) (0.5*j*(j+1));k++) {
System.out.print(k + " ");
}
System.out.println();
}

Categories

Resources