Mirrored Triangle - java

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("");
}

Related

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

Making a pattern with java using nested loops

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.

Asterisk in Nested loops, Java

I am trying to make my code print out the Asterisk in the image, you see below. The Asterisk are align to the right and they have blank spaces under them. I can't figure out, how to make it go to the right. Here is my code:
public class Assn4 {
public static void main(String[] args) {
for (int i = 0; i <= 3; i++) {
for (int j = 0; j <= i; j++) {
System.out.print("*");
}
for (int x = 0; x <= 1; x++) {
System.out.println(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print("*");
}
}
System.out.println();
}
}
Matrix problems are really helpful to understand loops..
Understanding of your problem:
1) First, printing star at the end- That means your first loop should be in decreasing order
for(int i =7;i>=0; i+=i-2)
2) Printing star in increasing order- That means your second loop should be in increasing order
for(int j =0;j<=7; j++)
Complete code:
for(int i =7;i>=0; i=i-2){ // i=i-2 because *s are getting incremented by 2
for(int j =0;j<=7; j++){
if(j>=i){ // if j >= i then print * else space(" ")
System.out.print("*");
}
else{
System.out.print(" ");
}
}
System.out.println();// a new line just after printing *s
}
Starting loops with 1 can sometimes help you visualize better.
int stopAt = 7;
for (int i = 1; i <= stopAt ; i += 2) {
for (int j = 1; j <= stopAt; j++) {
System.out.print(j <= stopAt - i ? " " : "*");
}
System.out.println();
}
Notice, how each row prints an odd number of *s ending at the line with 7. So, you start with i at 1 and go through 3 1+2, 5 3+2, and then stopAt 7 5+2.
The nested for loop has to print 7 characters always to make sure *s appear right aligned. So, the loop runs from 1 to 7.
Here the complete code:
for(int i = 0; i < 8; i++){
if( i%2 != 0){
for(int x = 0; x < i; x++){
System.out.print("*");
}
}else{
System.out.println();
}
}

Missing int in nested for loops

The following code:
for (i = 1; i <= 5; i++)
{
System.out.println();
for (j = 1; j <= i; j++) {
System.out.print(" ");
System.out.print(j);
}
}
produces:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Removing in the brace:
int i = 0;
int j = 0;
for (i = 1; i <= 5; i++)
{
System.out.println();
for (j = 1; j <= i; j++)
System.out.print(" ");
System.out.print(j);
}
produces:
2
3
4
5
6
I can understand that the nested for loop is iterating through the " " and printing them, then just printing one j, but I don't understand what happens to j = 1.
THe first time it goes through your for loop
it sets j = 1, checks j <= i
prints out the space
increments j, checks j < = i this fails because i = 1 and j = 2
exits the loop, and prints j and j = 2
For-loop without brace. Code you mentioned below:
for (j = 1; j <= i; j++)
System.out.print(" ");
System.out.print(j);
It is equal to the following code:
for (j = 1; j <= i; j++) {
System.out.print(" ");
}
System.out.print(j);
That's the problem, have a try and go ahead.
You can change it as follows:
for (j = 1; j <= i; j++) {
System.out.print(" ");
System.out.print(j);
}
The output will be expecte, like:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
I worked it out
System.out.println();
for (j = 1; j <= i; j++)
System.out.print(" ");
System.out.print(j);
The j is incremented j++ before being printed, as the loop has gone through an iteration befoe arriving at the System.out.print(j);
Since it only executes one statement, the value of j when it gets printed is one greater than the upper bound of your inner loop (2, 3, 4, 5, 6).

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