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).
Related
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("");
}
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'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(" ");
}
}
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.
i want to print a triangle/pyramid style like:
1
323
54345
7654567
here is my code:
int lines = 5;
for (int i = 1; i < lines; i++) {
for (int j = 1; j < lines-i; j++) {
System.out.print(" ");
}
for (int j = i; j > 1; j--) { //this for loop is my problem. any solution?
System.out.print(j);
}
for (int j = i; j < i+i; j++) {
System.out.print(j);
}
System.out.println();
}
what i got is
1
223
32345
4324567
i been studying codes while working at office and i think week long i still could not find a solution to this even i use search in Google.
i am only into enhancing my logic through conditionals and no heavy object oriented or recursion yet.
The problem in your first loop is a problem you figured out in your second one! (and it has something to do with the largest number in the loop)
for (int j = i; j > 1; j--) { //this for loop is my problem. any solution?
System.out.print(j);
}
Look at the numbers on the left of the pyramid. They start where the ones on the right end (every line of the pyramid is symmetrical). And the general formula for that number is i + i - 1, where i is the line number from your outer loop.
The second row starts at 2 * i - 1 = 2 * 2 - 1 = 3. The third row starts at 2 * 3 - 1 = 5 etc.
Your second inner loop should therefore look like this:
for (int j = i + i - 1; j > i; j--) {
System.out.print(j);
}
Here is the complete fixed source.
You have to start at the i-th odd number. This is i*2-1. And you stop at i. This also fixes a spacing difference introduced by changing it to lines = 4.
int lines = 4;
for (int i = 1; i <= lines; i++) {
for (int j = 1; j < lines-i+1; j++) {
System.out.print(" ");
}
for (int j = i*2-1; j > i; j--) { //this for loop is my problem. any solution?
System.out.print(j);
}
for (int j = i; j < i+i; j++) {
System.out.print(j);
}
System.out.println();
}
Run it here: http://ideone.com/AKsc1f
int lines = 4;
for (int i = 1; i <= lines; i++) {
for (int j = 1; j < lines-i+1; j++) {
System.out.print(" ");
}
//replace this
for(int j=0; j<i-1; j++) System.out.print(i*2-j-1);
System.out.print(i);
for(int j=; j<i-1;j++) System.out.print(i+j+1);
//==========
System.out.println();
}