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");
}
}
Related
My code print a tree with a trunk size of 2 spaces in an array, in the last space it needs also a floor, but i don't know how to implement this, so far my code works like this:
public static void main(String[] args) {
//up part
for(int i=1; i<=5; i++){
for(int k=1; k<=6-i+1; k++){
System.out.print(" ");
}
for(int j=1; j<=2*i-1; j++){
System.out.print("*");
}
System.out.println();
}
// trunk
for(int i=1; i<=2; i++){
for(int k=1; k<=5; k++){
System.out.print(" ");
}
for(int j=1; j<=1; j++){
System.out.print("||");
}
System.out.println();
}
System.out.println("_____||_____");
}
}
//floor
for (int i = 1; i <= 2 * 6; i++) {
System.out.print(i == 6 ? "||" : "_");
}
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
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();
}
}
I have to create a number pyramid using nested loops for homework. I am very new to nested loops and am still not quite clear on how they work completely. My objective is to make this pyramid using nested loops:
-----1-----
----333----
---55555---
--7777777--
-999999999-
however I have only been able to get this:
----------1
--------333--
------55555----
----7777777------
--999999999--------
I think I am on the right track but I am not sure where to go from here.
Here is the code I currently have:
public class NumberPyramid
{
public static void main(String [] args)
{
for(int i=1; i<=9; i+=2)
{
for (int j = 11; j > i; j--)
{
System.out.print("-");
}
for(int j=1; j<=i; j++)
{
System.out.print(i);
}
for (int j = 1; j < i; j++)
{
System.out.print("-");
}
System.out.println();
}
}
}
The part of printing the numbers in the center is correct.
Printing the - before and after the numbers is incorrect.
Notice that for any row, the number of - before and after the numbers should be the same. How many - to print for a number?
For 1, print 5 - before and after.
For 3, print 4 - before and after.
For 5, print 3 - before and after.
That's (11 - i) / 2. Put this loop before and after the center line and you're done.
for (int j = 0; j < (11 - i) / 2; j++) {
System.out.print("-");
}
You should remove two - on every second line
int index = 5;
for (int i=1; i<=9; i+=2) {
for (int j=0; j<index; j++) {
System.out.print("-");
}
for (int k=0; k<i; k++) {
System.out.print(i);
}
for (int j=0; j<index; j++) {
System.out.print("-");
}
System.out.println();
index--;
}
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.