nested for loops inverted half pyramid - java

for a school project, I am trying to make an inverted half pyramid
my code is currently this
public static void main(String[] args) {
int rows = 5;
for(int i = rows; i >= 1; --i) {
for(int j = 1; j <= i; ++j) {
System.out.print(j + " ");
}
System.out.println();
}
}
with this output:
12345
1234
123
12
1
desired output:
54321
=4321
==321
===21
====1

Update (based on the updated requirement):
You need a loop to print the = equal to (rows - row number) times.
public class Main {
public static void main(String[] args) {
int rows = 5;
for (int i = rows; i >= 1; --i) {
for (int j = i; j < rows; j++) {
System.out.print("=");
}
for (int j = i; j >= 1; --j) {
System.out.print(j);
}
System.out.println();
}
}
}
Output:
54321
=4321
==321
===21
====1
Original answer:
Your inner loop should be
for (int j = i; j >= 1; --j)
i.e. for each row, it should start with the row number (i.e. i) and go down up to 1.

It is straight forward you will have to change to things: your inner loop and you will have to move println statement inside the loop
//code
public static void main(String[] args){
int rows = 5;
for (int i = rows; i >= 1; --i){
for(int j = i; j >= 1; --j)
System.out.print(j + " ");
System.out.println();
}
}

Related

Can this problem be solved using nested loops?

Would it be possible to produce this layout with nested loops? I'm still new to nested to Java/loops and cannot solve this issue.
*****====+
*****===++
*****==+++
*****=++++
*****+++++
====++++++
===+++++++
==++++++++
=+++++++++
I'm having trouble looping through five times with the "*" character without allowing the "+" to increment.
Here is my code:
class Main {
public static void main(String[] args) {
for (int k = 4; k > 0; k--) {
System.out.print("*****");
for (int l = 0; l < k; l++) {
System.out.print("=");
}
for (int m = 0; m < 1; m++) {
for (int n = 0; n < m; n++) {
System.out.print("+");
}
}
System.out.println();
}
System.out.print("*****+++++");
}
}
there are could be multiple approaches to this problem, here is how I would think about it:
you need to display 9 lines of "something", so lets have top level loop:
for (int i=0; i<9; i++) {...}
now, each iteration of this loop you need to display X stars, Y equal signs, Z plus signs:
for (int i=0; i<9; i++) {
for (int j=0; j< #X# ; j++) System.out.print("*");
for (int j=0; j< #Y# ; j++) System.out.print("=");
for (int j=0; j< #Z# ; j++) System.out.print("+");
System.out.println();
}
now you need to determine rules how X,Y,Z are changed, here is the logic I came up with:
if (stars > 0 && equals == 0) {
stars = 0;
equals = 5;
}
equals--;
pluses++;
so, final code will look like:
public static void main(String[] args) throws InterruptedException {
int stars = 5; // initial state
int equals = 4;
int pluses = 1;
for (int i=0; i<9; i++) {
for (int j=0; j<stars; j++) System.out.print("*");
for (int j=0; j<equals; j++) System.out.print("=");
for (int j=0; j<pluses; j++) System.out.print("+");
System.out.println();
if (stars > 0 && equals == 0) {
stars = 0;
equals = 5;
}
equals--;
pluses++;
}
}
If you notice there is a pattern.
It prints a * starting from left to right.
It prints a + for each increasing number starting from right to left.
It prints a = instead of * if you are past mid point (first the mid point of left to right, then the mid point of top to bottom).
The logic can be applied as following,
class Main {
public static void main(String[] args) {
int max = 10;
int switchPoint = max - 1;
for(int i = 1; i <= max -1; i++) {
for(int j = 1; j <= max; j++) {
if(j > switchPoint)
System.out.print("+");
else if( i > max/2 || j > max / 2)
System.out.print("=");
else
System.out.print("*");
}
switchPoint--;
System.out.println();
}
}
}
/* Output:
*****====+
*****===++
*****==+++
*****=++++
*****+++++
====++++++
===+++++++
==++++++++
=+++++++++
*/
As you may notice the behavior of the "*" and "=" is different in the first five lines than in the next five lines, so you may either divide the loop in two loops or make a single one and check wether you are printing the first five lines or the last ones, that check may be done either by an if statement or in the loops conditions.
So the code will look like:
class Main {
public static void main(String[] args) {
for (int i = 1; i < 10; i++){
for (int j = 1; i <= 5 && j <= 5; j++){
System.out.print("*");
}
for (int k = 5; (i <= 5 && k > i) || (i > 5 && k > i-5); k--){
System.out.print("=");
}
for (int l = 1; l <= i; l++){
System.out.print("+");
}
System.out.println();
}
}
}
public class A {
public static void main(String[] args) {
for (int k = 4; k > 0; k--) {
System.out.print("*****");
for (int l = 0; l < k; l++) {
System.out.print("=");
}
for (int m = 0; m < 5 - k; m++) {
System.out.print("+");
}
System.out.println();
}
System.out.print("*****+++++");
System.out.println();
for (int s = 4; s > 0; s--) {
for (int k = 0; k < s; k++) {
System.out.print("=");
}
for (int l = 0; l < 5; l++) {
System.out.print("+");
}
for (int m = 0; m < 5 - s; m++) {
System.out.print("+");
}
System.out.println();
}
}
}

Trying to form a half triangle using a for loop, nothing is printing out

This is a practice exam question I was given to study for my java exam approaching...I was given the main method and cannot change the input, only alter the two other methods and their code. I need to print out
&
&&
&&&
&&&&
&&&&&
&&&&&&
I think I have written my for loop wrong to create the blank spaces, I cannot seem to get this write with the main method I have been give, any ideas?
public static void main(String[] args) {
int size = 6;
char c = '&';
for (int i = 1; i < size + 1; i++) {
drawBlanks(size, size - i);
drawChars(size, size - i, c);
System.out.println();
}
System.out.println();
}
public static void drawChars(int size, int i, char c) {
for (int j = size; j < 1; j--) {
System.out.print(c);
}
}
public static void drawBlanks(int size, int i) {
for (int k = 0; k <= i; k++) {
System.out.print(" ");
}
}
You have a problem in this loop :
for(int j = size; j < 1; j--)
Instead change it to :
for (int j = size; j > i; j--) {
//-------------------^_^
j should be > to i not j < to 1
Alternative solution within one loop:
public class Main {
public static void main(String[] args) {
int rowCount = 10;
int whiteSpaceCount = rowCount - 1;
for (int i = 0; i < rowCount; i++) {
for (int j = 0; j < rowCount; j++) {
char ch = ' ';
if (j >= whiteSpaceCount)
ch = '&';
System.out.print(ch);
}
System.out.println();
whiteSpaceCount = rowCount - (i + 2);
}
}
}
You need to change j < 1; to j > 1;, After that your output will be,
Output:
&&&&&
&&&&&
&&&&&
&&&&&
&&&&&
&&&&&
To get your expected output change j > 1 to j > i.
public static void main(String[] args) {
int size = 6;
char c = '&';
for (int i = 1; i < size + 1; i++) {
drawBlanks(size, size - i);
drawChars(size, size - i, c);
System.out.println();
}
System.out.println();
}
public static void drawChars(int size, int i, char c) {
for (int j = size; j > i; j--) {
System.out.print(c);
}
}
public static void drawBlanks(int size, int i) {
for (int k = 0; k <= i; k++) {
System.out.print(" ");
}
}
Output:
&
&&
&&&
&&&&
&&&&&
&&&&&&

Using nested for loops to produce multiple lines of String output

I am learning Java for myself and have following problem:
Task: Write (explicitly) nested for loops to produce the following output:
5
5 5
5 5 5
5 5
5
My idea:
public class Exercises {
public static void main (String [] args) {
for (int line = 1; line <= 3; line++) {
for (int i = 1; i<= -2*line+6; i++){
System.out.print(" ");
}
System.out.println("5");
}
for ( int line = 4; line <= 5; line ++){
for (int i = 1; i <= 2*line-6; i++){
System.out.print(" ");
}
System.out.println("5");
}
}
}
My output:
5
5
5
5
5
I don't know how to get nested loops there. Can you give me not the answer, but some tips?
Try this solution:
/**
*
* #author Adil
*/
public class Exercises {
public static void main (String [] args) {
for (int i = 1; i <= 3; i++) {
for(int s = 4; s > i; s--) {
// add spacing
System.out.print(" ");
}
for (int j = 1; j < i; j++) {
//display/add star
System.out.print("5");
}
// add new line
System.out.println("");
}
for (int i = 1; i <= 3; i++) {
for (int s = 1; s < i; s++) {
// add spacing
System.out.print(" ");
}
for (int j = 4; j > i; j--) {
//display/add star
System.out.print("5");
}
// add new line
System.out.println("");
}
}
}
It will produce the following output:
5
55
555
55
5
Try this solution give below:
public class Exercises {
public static void main(String[] args) {
int noOfColumns = Integer.parseInt(args[0]); // Enter no of 5's in last
// column
int noOfRows = noOfColumns;
int index = noOfColumns;
int mid = noOfRows / 2;
for (int i = 0; i < noOfRows; i++) {
if (i <= mid) {
index = index - 2;
} else {
index = index + 2;
}
for (int j = 0; j < noOfColumns; j++) {
if (j % 2 == 0 && j >= index) {
System.out.print("5");
} else {
System.out.print(" ");
}
} // End of inner loop
System.out.println("");
} // End of outer loop
}
}

java pyramid with multiple of 2 not working

I am suppose to create a pyramid and multiply each number by two until it reach the middle then divide by two such as shown in example below.
However after writing my code I am unable to have the numbers double (i*2) then once it reaches the center it divides by two until the it becomes 1
My output:
package question5;
public class Pyramid {
public static void main(String[] args) {
int x = 5;
int rowCount = 1;
System.out.println("Here Is Your Pyramid");
//Implementing the logic
for (int i = x; i > 0; i--)
{
//Printing i*2 spaces at the beginning of each row
for (int j = 1; j <= i*2; j++)
{
System.out.print(" ");
}
//Printing j value where j value will be from 1 to rowCount
for (int j = 1; j <= rowCount; j++)
{
System.out.print(j+" ");
}
//Printing j value where j value will be from rowCount-1 to 1
for (int j = rowCount-1; j >= 1; j--)
{
System.out.print(j+" ");
}
System.out.println();
//Incrementing the rowCount
rowCount++;
}
}
}
this is the weird output of it
This is working... You can use math.pow method.
public class test {
public static void main(String[] args) {
int x = 5;
int rowCount = 1;
System.out.println("Here Is Your Pyramid");
//Implementing the logic
for (int i = x; i > 0; i--)
{
//Printing i*2 spaces at the beginning of each row
for (int j = 1; j <= i*2; j++)
{
System.out.print(" ");
}
//Printing j value where j value will be from 1 to rowCount
for (int j = 0; j <= rowCount-1; j++)
{
System.out.printf("%2d", (int)Math.pow(2, j));
}
//Printing j value where j value will be from rowCount-1 to 1
for (int j = rowCount-1; j >= 1; j--)
{
System.out.printf("%2d", (int)Math.pow(2, j-1));
}
System.out.println();
//Incrementing the rowCount
rowCount++;
}
}
}
Right now you're outputting j, a number from 1 to 5. You want to output 2j-1 instead, which is easy: 1 << (j - 1).
You also need to right-adjust the number. Your print statement then becomes:
System.out.printf("%4d", 1 << (j - 1));

Simple Java Loop Question

Assume that the int variables i and j have been declared, and that n has been declared and initialized.
Using for loops (you may need more than one), write code that will cause a triangle of asterisks of size n to be output to the screen.
For example, if the value of n is 4, the output should be
*
**
***
****
I won't do your homework, so here is a hint:
The first line is always 1 element long.
The last line is always N elements long.
The are a total of N lines.
Surely, with the above, you can create the necessary program.
Just for fun - single for-loop solution:
public void doIt(int n) {
String temp = String.copyValueOf(char[n]);
for (int i = 1; i <= n; i++)
System.out.println(temp.substring(n-i).replace((char) 0, 'x'));
}
And some recursion - zero for-loop solution:
public void doItAgain(int n, String display) {
if (n==0) return;
System.out.println(display);
doItAgain(n-1, display+'x');
}
(call it with doItAgain(4,"x") for your example)
My answer:
public class loop1
{
public static void main(String[] args)
{
for(int i = 0; i < 4; i++)
{
for(int j = 0; j <= i; j++)
System.out.print("*");
System.out.println();
}
}
}
In case you're in school/college and more interested in getting some, more power to you buddy:
for(int i = 0; i < n; i++)
{
for(int j = 0; j <= i; j++)
System.out.print("*");
System.out.println();
}
public class ForLoop {
public static void main(String[] args) {
for(int i = 0;i <= 9;i++){
for(int j = 1;j <= i;j++){
System.out.print("*");
}
System.out.println("\n");
}
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j <= i; j++)
System.out.print("*");
System.out.println();
}
simple, easist way to do it using main method --> public static void main (Strings [] args){
for(int i = 1; i <= max; i++){
for(int j = 1; j <= i; j++){
System.out.print("*");
}
System.out.println(" ");
}
}

Categories

Resources