Upside down right triangle in Java - java

I need to do this:
*****
****
***
**
*
and I have this code:
for (int i=0; i<5; i++)
{
for (int j=5; j>i; j--)
{
System.out.print("*");
}
System.out.println("");
which outputs this:
*****
****
***
**
*
I cant figure out how to implement the spaces. Any help appreciated.

You need to use two for-loops: one for the number of spaces and one for the number of *:
for (int i = 0; i < 5; i++) {
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
for (int j = i; j < 5; j++) {
System.out.print("*");
}
System.out.println();
}
Java 8 solution:
IntStream.range(0, 5).forEach(i -> {
IntStream.range(0, i).forEach(j -> System.out.print(" "));
IntStream.range(i, 5).forEach(j -> System.out.print("*"));
System.out.println();
});

Here's a solution with one less loop than the other answers, if you really want to wow the person who's grading your assignment:
for (int y = 0; y < 5; y++) {
for (int x = 0; x < 5; x++) {
System.out.print((x >= y) ? "*" : " ");
}
System.out.println();
}

Just print k number of spaces before start of every line.
To solve this kind of problems, it will be easy if you break it down and observe the pattern.
***** 0 space
**** 1 space
*** 2 spaces
** 3 spaces
* 4 spaces
After taking note of this pattern, you ask yourself will you be able to print this?
0*****
1****
2***
3**
4*
We see that the number is similar to the start of every line. Hence we could make use of variable i. (your outer loop counter) and we have..
for (int i=0; i<5; i++){
System.out.println(i);
for (int j=5; j>i; j--){
System.out.print("*");
}
System.out.println("");
}
Now, you just have to convert your numbers at the start of every line to the number of spaces and you have..
for (int i=0; i<5; i++){
for(int k=0; k<i; k++) //using i to control number of spaces
System.out.println(" ");
for (int j=5; j>i; j--){
System.out.print("*");
}
System.out.println("");
}

int count = 6;
for(int i=0; i<=count; i++) {
for(int j=0; j<i; j++) {
System.out.print(" ");
}
for (int j = 0; j <= (count - i); j ++) {
System.out.print("*");
}
System.out.println();
}
you can pass your value into count, its works pretty nice.

Related

Java: Formatting nested loops?

I am pretty new to Java and I wanted to make a staircase effect using loops, but adding an increasing amount of spaces to the string as you go down each row.
Heres my code-
for(int i = size; i>0; i--) {
for(int k = 1; k>size-1; k++) {
output+=" ";
}
for(int j = i; j>0; j--) {
output+=let;
}
output+="\n";
}
return output;
}
Ultimately the goal is to have it print this:
22222
2222
222
22
2
But mine prints this:
22222
2222
222
22
2
I'm sorry, I know this is beginner stuff but I don't know where else to go. Any help is appreciated. Thanks.
You need size - i spaces each time, so change for(int k = 1; k>size-1; k++) { to for(int k = 0; k<size-i; k++) {.
for(int i = size; i>0; i--) {
for(int k = 0; k<size-i; k++) {
output+=" ";
}
for(int j = i; j>0; j--) {
output+=let;
}
output+="\n";
}
Try this code:
int size=5;
for(int i = size; i>0; i--) {
for(int k = size-i; k>0; k--) {
System.out.print(" ");
}
for(int j = i; j>0; j--) {
System.out.print("2");
}
System.out.println("");
}
your second for loop condition is wrong.
convert for(int k = 1; k>size-1; k++)into for(int k = size-i; k>0; k--)
Try this:
for(int i=0; i<n; i++)
{
for(int j = 2 * (n-i); j >= 0; j--)
{
System.out.print(" ");
}
for(int j = 0; j<=i; j++)
{
System.out.print("2 ");
}
System.out.println();
}
Note that the int n is the number of lines you want to print
n = 5 -> 5 lines

Countdown Pyramid error

I'm trying to get a pyramid that counts:
1
121
12321
1234321
123454321
this is my code
for (int i=5; i>=1; i--){
for (int j=5; j>= (i-1); j--){
System.out.print("");
}
for (int j=i; j<=5; j++){
System.out.print(j);
}
System.out.println();
but this keeps giving my the output
1
222
33333
4444444
555555555
How can i get the right side of my pyramid to count down
Here is a working implementation. Your approach to the problem seems good to me, but the third inner loop had a problem. Instead of counting from 1 upwards, it was also counting down. Instead just use the approach of a single outer loop, to cover each row of the pyramid, along with three inner loops. The first loop can print spaces, and the next two will count, then up, respectively.
int row = 1;
for (int i=1; i<=5; i++) {
for (int o=1; o<=5-i; o++) {
System.out.print(" ");
}
for (int k=1; k<=i; k++) {
System.out.print(k);
}
for (int k = i-1; k >= 1; k--){
System.out.print(k);
}
System.out.println();
row++;
}
Output:
1
121
12321
1234321
123454321
Demo here:
Rextester
This is a method to print pyramid:
public void print(int height) {
for (int i=0; i<height; i++) {
for (int j=height; j>=1; j--) {
if (j <= i+1) {
System.out.print(j);
} else {
System.out.print(" ");
}
}
for (int j=2; j<=height; j++) {
if (j <= i+1) {
System.out.print(j);
} else {
System.out.print(" ");
}
}
System.out.print("\n");
}
}

How to draw X using arrays?

Does anyone have an idea how to print X from given number?
e.g.:
given number 5.
So, I should print
X000X
0X0X0
00X00
0X0X0
X000X
This is my code, still missing something on it
public static void drawX(int number){
int[][] draw = new int[number][number];
for(int i = 0; i< number; i++){
for(int k = 0; k<=i; k++){
System.out.print(" ");
}
for(int j = number-1; j>i; j--){
if(j == number-1 || j == i+1)
System.out.print("X ");
else
System.out.print(" ");
}
System.out.println();
}
for(int i = 0; i< number; i++){
for(int v = number; v>i; v--){
System.out.print(" ");
}
for(int j = 0; j<i; j++){
System.out.print("X ");
}
System.out.println();
}
}
See comments on your code below, highlighting some issues I've noticed. It probably won't solve everything straight away, but it will give you a push in the right direction I hope.
public static void drawX(int number){
// draw is never used.
int[][] draw = new int[number][number];
for(int i = 0; i< number; i++){
for(int k = 0; k<=i; k++){
// Here we print a " " even for k == i.
// Are you sure you want k <= i ?
// If you change it, dont forget to also change
// stop clause in next loop.
System.out.print(" ");
}
for(int j = number-1; j>i; j--){
if(j == number-1 || j == i+1)
// j == number -1 prints X only in the last column
// Maybe you wanted number - i - 1?
// j == i+1 means you "skip" the ith element.
// Why the extra space after X?
System.out.print("X ");
else
// Why two spaces here?
System.out.print(" ");
}
System.out.println();
}
for(int i = 0; i< number; i++){
for(int v = number; v>i; v--){
System.out.print(" ");
}
for(int j = 0; j<i; j++){
// here you need to do very similar logic to
// what you did in previous loop, when printing first
// 'number' lines.
System.out.print("X ");
}
System.out.println();
}
}

How do i draw an Arrow in Java?

Here is what I am trying to create:
*
**
***
****
*********
****
***
**
*
Here is what i have created:
*
**
***
****
**********
****
***
**
*
Here is my code:
for(int i = 1; i <= 5; i++){
for(int j = 1; j<=i; j++){
System.out.print("*");
}
System.out.println();
if(i == 4){
for(int f = 0; f < 5; f++){
System.out.print("*");
}
}
}
for(int i = 1; i <= 5; i++){
for(int j = 4; j>=i; j--){
System.out.print("*");
}
System.out.println();
}
I dont know how to indent the tail part, please dont give me the answer, just tell me where the problem is and I will try to do it. Thank you!
Given the shape you have provided, and assuming it's going to be output in a monospace font, we can draw it on a grid.
Splitting it visually into 2 sections, we can see that there are 2 main modes.
The tail (green), and the head (red).
There's also a second mode. where the number of stars in the head increase and decrease.
The size of the tail could potentially vary, as could the size of the head, and it would still be a shape we recognise as an arrow.
When outputting text, the easiest iteration order is generally left to right, up to down, unless dealing with right to left languages, or vertical reading, I'm going to assume western culture for the output, as that is what's been popularised in programming output streams.
So, the question is, how best to build up the strings required for output.
Given the format that the output is going to be on a stream,
width is going to be your outside loop, and tail/length your inside loop.
The code you provided uses 10 for width, by splitting it into 2 groups of 5, with the second one being offset by one.
for(int i = 1; i <= 5; i++){
for(int j = 1; j<=i; j++){
System.out.print("*");
}
System.out.println();
if(i == 4){
for(int f = 0; f < 5; f++){
System.out.print("*");
}
}
}
for(int i = 1; i <= 5; i++){
for(int j = 4; j>=i; j--){
System.out.print("*");
}
System.out.println();
}
so translating your code back to the drawings, you loop over width, and on the last iteration output the line of stars for the tail.
What you need to do, is output a space on all other lines.
There are a variety of ways to do so, you could output the character, or use one of the fixed width formatting functions using printf.
But given your current code, the minimal difference will be to output a space character, when the width iteration for the increasing mode is not 4, and for the loop of width - 1.
Looking at your end result, your tail is being printed after your head. That needs to move earlier.
Your tail is too long, need to offset that by 1.
And you need to insert pink and brown sections by printing spaces, the same amount of times as you output the tail, whenever you do not output the tail.
You should add another inner for-loop that prints " " before the for-loop that prints "*".
You can use printf method to tell Java print in specific format. For example, you need 5 spaces prefixed then print your text:
System.out.printf("%5s","hello");
To get your desired output, try the below code. Note: There may be better solutions.
for (int i = 1; i <= 5; i++) {
if (i != 5) // middle line should not be prefix with spaces
// empty 5 spaces before starting the loop
System.out.printf("%5s", "");
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
if (i == 4) {
for (int f = 0; f < 5; f++) {
System.out.print("*");
}
}
}
for (int i = 1; i <= 5; i++) {
// print 5 space before
System.out.printf("%5s", ""); // <== note the printf here with empty string
for (int j = 4; j >= i; j--) {
System.out.print("*");
}
System.out.println();
}
Here's a method you can use to print the arrow:
private static void printArrow (final int min, final int max, final int tip)
{
final int numSpaces = tip - max - 1;
// print the top
for (int i = min; i <= max; i++)
{
for (int j = 0; j < numSpaces; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.println();
}
// print the tip
for (int i = 0; i < tip; i++)
{
System.out.print("*");
}
System.out.println();
// print the bottom
for (int i = max; i >= min; i--)
{
for (int j = 0; j < numSpaces; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.println();
}
}
Call it from main with:
public static void main(String[] args)
{
// this will print the arrow in your question
printArrow(1, 4, 9);
}
Another Example:
printArrow(1, 10, 19);
Outputs:
*
**
***
****
*****
******
*******
********
*********
**********
*******************
**********
*********
********
*******
******
*****
****
***
**
*
Here is my answer. You just need to first insert the space and then print * until you reach the middle line. If you want to make it dynamic, just replace the boundary value for k dynamically the same as the boundary for the f:
public static void main(String[] args) {
int k = 1;
int m = 0;
for (int i = 1; i <= 5; i++) {
m = i - 1;
while (k != 6 && m != 4) {
System.out.print(" ");
k++;
}
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
if (i == 4) {
for (int f = 0; f < 5; f++) {
System.out.print("*");
}
}
k = 1;
}
k = 1;
for (int i = 1; i <= 5; i++) {
while (k != 6) {
System.out.print(" ");
k++;
}
for (int j = 4; j >= i; j--) {
System.out.print("*");
}
System.out.println();
k = 1;
}
}
EDIT
I realized the output is one* behind. So just need to change the boundary value of k from 5 to 6 (from the value of boundary for f to the value of boundary for f+1)
The output looks like this:
for (int i = 1; i <= 5; i++) {
if (i!=5) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
if (i == 4) {
for (int f = 0; f < 5; f++) {
System.out.print("*");
}
}
}
for (int i = 1; i <= 5; i++) {
System.out.print(" ");
for (int j = 4; j >= i; j--) {
System.out.print("*");
}
System.out.println();
}

How to transform a 5x5 block into a diagonal box in java

I have the code:
for (int i = 0 ; i < 5; i++){
for (int j = 0 ; j < 5; j++) System.out.print("*");
System.out.println();}
which gives:
*****
*****
*****
*****
*****
How do I transform it into:
*****
****
***
**
*
you need to loop to 5 - i times in inner loop
Don't be scared of testing and experimenting different results.
Updated:
OK. I just tested things here and tried this code in Obj-C and it did print exactly what you want:
int i,j;
for(i=5; i>=1; i--)
{
for(j=1; j<=i; j++)
{
printf("*");
}
printf("\n");
}
The Java translation for that would be:
for(int i=5; i>=1; i--)
{
for(j=1; j<=i; j++)
System.out.print("*");
System.out.println();
}
The following code will fix your problem
for (int i = 0 ; i < 5; i++)
{
for (int j = 5-i ; j <= 5; j++)
System.out.print("*");
System.out.println();
}

Categories