I want to make a program which gives this output:
*
**
***
****
***
**
*
where the maximum number of stars is 'n'. however, something is wrong with my code here. help will be appreciated.
class test
{
public static void main(int n)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print("*");
}
for(int x=n;x>=1;x--)
{
System.out.print("*");
}
System.out.println();
}
}
}
You're almost there - think of it as an upright triangle followed by an upside down triangle. In the first one, there are N rows, each row having an increasing number of asterisks, from one up to N. In the second one, each row has a decreasing number of asterisks, from N-1 down to 1:
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < i; ++j) {
System.out.print('*');
}
System.out.println();
}
for (int i = n - 1; i >= 1; --i) {
for (int j = 0; j < i; ++j) {
System.out.print('*');
}
System.out.println();
}
This could, of course, be made much more elegant by extracting the inner for loop which creates each row to its own method:
private static void printRow (int i) {
for (int j = 0; j < i; ++j) {
System.out.print('*');
}
System.out.println();
}
private static void main (String[] args) {
for (int i = 1; i <= n; ++i) {
printRow(i);
}
for (int i = n - 1; i >= 1; --i) {
printRow(i);
}
}
Related
I have an exercise where I have to print a christmas tree.
public class ChristmasTree {
public static void main(String[] args) {
int size = 6;
printChristmasTree(size);
}
public static void printChristmasTree(int size) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < (2 * i + 1); j++) {
System.out.print("*");
}
System.out.println();
}
for (int k = 0; k < 1; k++) {
System.out.print("**");
System.out.println();
}
System.out.println();
}
}
What I get is:
*
***
*****
*******
*********
***********
**
How can I fix it? How should I change this loop to move that trunk to the center of that tree?
Change
for (int k = 0; k < 1; k++) {
System.out.print("**");
System.out.println();
}
to
for (int k = 0; k < size; k++) {
System.out.print(" ");
}
System.out.print("**");
You need to loop to find number of spaces from size, and place it accordingly. Adjust the loop variable k if it is off a bit.
You said christmass tree so I guessed that you want the tree trunk to be in different lines. If it's what you want it's the code here:
for (int k = 0; k < 2; k++) {
for (int l = 0; l <= size; l++) {
if (l != size)
System.out.print(" ");
else
System.out.println("*");
}
System.out.println();
}
If you want in the same line here you are:
for (int l = 0; l <= size; l++) {
if (l != size)
System.out.print(" ");
else
System.out.print("**");
}
System.out.println();
I'm missing by just a little bit. What I want:
*******
*****
***
*
***
*****
*******
What I'm getting
*******
*****
***
*
*
***
*****
*******
The code
public class HD404 {
public static void main(String[] args) {
int N = StdIn.readInt();
int x = N*2-1;
for (int i = 0; i < N; i++) {
for (int j = i; j > 0; j--) {
StdOut.print(" ");
}
for (int k = 0; k < x; k++) {
StdOut.print("*");
}
x-=2;
StdOut.println();
}
x = 1;
for (int i = 0; i < N; i++) {
for (int j = i; j < N-1; j++) {
StdOut.print(" ");
}
for (int k = 0; k < x; k++) {
StdOut.print("*");
}
x += 2;
StdOut.println();
}
}
}
Right now I'm mostly just guessing and I just can't pin point my error. What am I missing here?
The problems lays with the second part of your code where you ask to draw one star and you start at zero where you should start at one.
Solution
x = 1;
for (int i = 0; i < N; i++)
should be replaced with
x = 3;
for (int i = 1; i < N; i++)
The problem is that you are starting to draw the bottom of the hourglass with 1 asterisk (x = 1) instead of 3.
The second issue is that the bottom of the hourglass only has N-2 lines, not N-1 so the loop should start at 1 instead of 0. This is because the line with a single asterisk was already drawn in the upper-half.
Corrected code:
public static void main(String[] args) {
int N = StdIn.readInt();
int x = N*2-1;
for (int i = 0; i < N; i++) {
for (int j = i; j > 0; j--) {
StdOut.print(" ");
}
for (int k = 0; k < x; k++) {
StdOut.print("*");
}
x-=2;
StdOut.println();
}
x = 3; // <-- not 1 here, the first line has 3 asterisks
for (int i = 1; i < N; i++) { // <-- i starts at 1 because the first line was already drawn in the upper half
for (int j = i; j < N-1; j++) {
StdOut.print(" ");
}
for (int k = 0; k < x; k++) {
StdOut.print("*");
}
x += 2;
StdOut.println();
}
}
As a side-note, you could rewrite this code a lot shorter by making the following observations:
There are x lines to draw so we can loop from 0 to x included (to respect the symmetry) and skip the middle line so as not to draw it twice
For every line, there are x columns to draw and it is either a space or a *.
For every line, * is drawn only if the current column is between min(i, x-i) and max(i, x-i) (if we're in the upper-part, i < x-i and if we're in the bottom-part, i > x-i).
Code:
public static void main(String[] args) {
int N = 4;
int x = 2 * N - 1;
for (int i = 0; i <= x; i++) {
if (i == N) continue; // skip the middle-line for it not to be drawn twice
for (int j = 0; j < x; j++) {
System.out.print(j >= Math.min(i, x-i) && j < Math.max(i, x-i) ? "*" : " ");
}
System.out.println();
}
}
Sample output:
*******
*****
***
*
***
*****
*******
The easiest way I can think up is probably to prevent the last iteration of your first outer loop, that way you'll prevent the first single star line to be shown.
I would probably do it this way:
for(int i = 0; i < N && x > 1; i++)
{
/*Code of the first inner loop*/
}
For those whose still looking for a simpler and lesser code regarding hourglass challenge. This contains 2 for loops only.
You may use this as reference.
public static void hourGlass(int size) {
// 2 for loops only
int dimension = (size * 2) - 1, space = 0, stars = size - 1, printed = 0;
for(int i=0; i<dimension; i++) {
int actual = space;
for (int j=dimension; j > 0; j--) {
if(actual > 0) {
System.out.print(" ");
actual--;
}
else {
System.out.print("*");
if(stars==printed) {
actual = space;
printed = 0;
} else {
actual = 1;
printed++;
}
}
}
if(i <= size-2) { // will pattern spaces and stars from top to middle
space++;
stars--;
}
else { // will pattern spaces and stars from middle to top
space--;
stars++;
}
System.out.println();
}
}
public class NestedForLoop {
private void generateNumberPyramid(int num) {
int length = num + 1;
for(int i = 1; i < length; i++) {
for(int j = 0; j < i; j++) {
System.out.print(j+1);
}
System.out.println();
}
}
public static void main(String[] args) {
NestedForLoop nfl = new NestedForLoop();
nfl.generateNumberPyramid(4);
}
}
The output is as follows:
1
12
123
1234
The intended output should be:
1
22
333
4444
What could be going wrong?
Change System.out.print(j+1); for System.out.print(i);
The value of i corresponds to each row. i=1 refers to the first row, i=2 refers to the second row and so on. Therefore in your for loop, make the following change:
for(int i = 1; i < length; i++) {
for(int j = 0; j < i; j++) {
System.out.print(i);
}
System.out.println();
}
Ideone link: http://ideone.com/5g0xWT
System.out.print(i) instead of System.out.print(j+1)
Your problem is in the nested for loop:
Change:
for(int j = 0; j < i; j++) {
System.out.print(j+1);
}
To:
for(int j = 0; j < i; j++) {
System.out.print(i);
}
Since you are iterating the ROWS, you should use i and not (j+1). Doing this will simply iterate what you want, otherwise it will keep adding one to each number. so just like this:
for(int i = 1; i < length; i++) {
for(int j = 0; j < i; j++) {
System.out.print(i);
}
System.out.println();
}
I have some problem printing a reverse triangle I want to achieve, like this pattern:
******
***
*
But my goal is to achieve that pattern with this numbers:
333221
221
1
So, this is my code so far:
int x = 1;
for(int r=0;r<3;r++)
{
x=x+r;
for(int c=0;c<x;c++)
{
System.out.print("*");
}
x+=1;
System.out.println();
}
Which the output is upright like this:
*
***
******
I want to make the pattern reverse with the numbers as it shown above.
Can anyone give me some idea how to deal with it? Thanks!
This is how i would do it:
for (i = 3; i > 0; i--) {
for (j = i; j > 0; j--) {
for (c = j; c > 0; c--) {
System.out.print(j);
}
}
System.out.println();
}
first loop: you want to print 3 lines;
second loop: each line has i distinct numbers
third loop: print number j j times
You just need to reverse the order of your loops and decrement x instead of incrementing it. I change the code a bit though :
int level = 3;
for(int r=level ; r>0 ; r--) {
for(int c=r ; c>0 ; c--)
for (int x=0 ; x<c ; x++)
System.out.print("*");
System.out.println();
}
int depth = 3;
for (int r = depth + 1; r >= 0; r--) {
for (int c = 0; c < r; c++)
for (int b = 0; b < c; b++)
System.out.print("*");
System.out.println();
}
#Test
public void reverseTriangle(){
int num = 3;
for (int i = num; i > 0; i--) {
this.draw(i);
System.out.println();
}
}
private void draw(int num) {
int total = 0;
for (int i = 0; i <= num; i++) {
total = total + i;
}
for (int i = 0; i < total; i++) {
System.out.print("*");
}
}
Here is what the shapes should look like :
Here is my code so far:
public class Diamonds {
public static void main(String[] args) {
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");
}
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");
}
}
}
I am having trouble getting the second shape
In order to not spoil your fun with this problem, I will explain what you need to do without writing any code.
To get the second shape in there, you would need to add two additional nested for loops into each of the two "outer" loops that you already have.
Loops number three will produce a fixed number of spaces. Note that the distance between the right edge of the first shape and the left edge of the second shape is constant, so your third loops will be easy to code up.
Loops number four will loop like your first loop, but they would change places: the first inner loop from the first outer loop will be the forth inner loop in the second outer loop, and vice versa.
By examining the shape on the right, we can notice that for each N asterisks on the line in the left shape, the right one has 10 - N, so, taking your code and extending it, we can get:
public class Diamonds {
public static final String SPACE = " ";
public static void main(String[] args) {
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(SPACE);
for (int j = 0; j < 10 - i; j++)
System.out.print("*");
System.out.print("\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(SPACE);
for (int j = 0; j < 10 - i; j++)
System.out.print("*");
System.out.print("\n");
}
}
}
And if we extract some common code:
public class Diamonds {
public static final String SPACE = " ";
public static void main(String[] args) {
for (int i = 1; i < 10; i += 2) {
drawLine(i);
System.out.print("\n");
}
for (int i = 7; i > 0; i -= 2) {
drawLine(i);
System.out.print("\n");
}
}
private static void drawLine(int i) {
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(SPACE);
for (int j = 0; j < 10 - i; j++)
System.out.print("*");
}
}
public class ReverseDiamond {
public static void main(String[] ar) {
int rows = 10;
ReverseDiamond diamond = new ReverseDiamond();
for(int i = 0; i < rows; i++)
diamond.printLine(rows, i);
for(int i = rows - 2; i >= 0; i--)
diamond.printLine(rows, i);
}
private void printLine(int rows, int currRow) {
for(int space = 1; space <= currRow; space++)
System.out.print(" ");
for(int star = 1; star < 2 * (rows - currRow); star++)
System.out.print("*");
System.out.println();
}
}
You may like that :
public class Diamonds {
public static void main(String[] args) {
int totalStars = 9;
int rows = 9;
for (int r = 0,stars=-1,gap=totalStars; r < rows; r++ ) {
stars+= (r<=rows/2) ?2:-2;
gap=totalStars-stars;
printChars(' ', gap);
printChars('*', stars);
printChars(' ', gap);
int gap2=stars+1;
int stars2=gap+1;
printChars(' ', gap2);
printChars('*', stars2);
printChars(' ', gap2);
System.out.println();
}
}
private static void printChars(char c ,int times) {
for (int i = 0; i < times; i++) {
System.out.print(c);
}
}
}
try this :
public static void main(String[] args) {
for (int i = 9; 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");
}
for (int i = 2; 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");
}
}
output :
*********
*******
*****
***
*
***
*****
*******
*********