Simple Java Loop Question - java

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(" ");
}
}

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();
}
}
}

nested for loops inverted half pyramid

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();
}
}

Need to invert a loop in java

import java.util.Scanner;
public class PrintVshape {
public static void main(String [] args){
Scanner inputDevice = new Scanner(System.in);
for(int i=0;i<5;i++){
for(int k=6;k>i;k--){
System.out.print("x");
}
System.out.print("V");
for(int j=7;j>i;j--){
System.out.print("p");
}
System.out.print("V");
System.out.print("\n");
}
}
}
This prints out the following:
xxxxxxVpppppppV
xxxxxVppppppV
xxxxVpppppV
xxxVppppV
xxVpppV
This is a homework problem. It's the x's that I'm stuck on. I need to invert them so they become more instead of fewer.
The loop you have for x says that run it from 6 to i and i moves from 0 to 5;
for(int k=6;k>i;k--){
System.out.print("x");
}
Which means x will be printed 6 times , 5 times .. ..
Correct the loop for x and it will be fine.
You need to loop on k up to some increasing value, such as i, or in this case, i+2, to get the range between two xs in the first row to six xs in the last:
for (int k = 0; k < i+2; k++) {
System.out.print("x");
}
Scanner inputDevice = new Scanner(System.in);
for(int i=0;i<5;i++){
for(int k=0;k<i+2;k++){
System.out.print("x");
}
System.out.print("V");
for(int j=7;j>i;j--){
System.out.print("p");
}
System.out.print("V");
System.out.print("\n");
}
}
result:
xxVpppppppV
xxxVppppppV
xxxxVpppppV
xxxxxVppppV
xxxxxxVpppV
i+2 in k cycle, mean how many times it will print x on beginnig (if you want start with xx then i=0+2)
I am not quite sure, is this the thing you are after now?
public static void main(String[] args) {
int N = 8;
printV(N);
}
private static void printV(int N) {
for (int i = 0; i < N; i++) {
for (int k = 0; k < i; ++k) {
System.out.print("x");
}
System.out.print("V");
for (int j = 0; j < (N - i - 1) * 2; ++j) {
System.out.print("p");
}
System.out.print("V");
for (int k = 0; k < i; ++k) {
System.out.print("x");
}
System.out.print("\n");
}
}
Output:
VppppppppppppppV
xVppppppppppppVx
xxVppppppppppVxx
xxxVppppppppVxxx
xxxxVppppppVxxxx
xxxxxVppppVxxxxx
xxxxxxVppVxxxxxx
xxxxxxxVVxxxxxxx

Creating a double mirrored triangle

I need help making a mirrored triangle like this:
* *
** **
*** ***
********
I can get each one seperatly, but I can't combine them.
public static void main(String[] args){
for( int i = 1; i <= 5; i++ ){
for( int j = 0; j < i; j++ ){
System.out.print("*");
}
System.out.println();
}
for(int i = 0; i < 6; i++)
{
for(int j = 5; j > 0; j--)
{
if(i < j)
System.out.print(" ");
else
System.out.print("*");
}
System.out.println();
}
}
You need to track the position from both sides to be able to show * at correct location. Here is the solution:
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 10; j++) {
if (j <= i || j > 10 - i) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
You can do it using this code.
public class Test {
public void sizeOfTri(int triSize) { //Number of lines
int line = 1;
for(int i = 0; i < triSize; i++) { //Handles Each line
int temp = triSize * 2;
for(int j = 1; j < temp; j++) { //Handles Each space on the line
if(j <= line && j == triSize || j >= temp - line && j == triSize) { //For the very last line because it needs an extra *
System.out.print("**");
} else if(j <= line || j >= temp - line) { //For normal lines
System.out.print("*");
} else if(j == triSize) {
System.out.print(" "); //For the second to last line because it needs an extra space to make it look mirrored
} else { //For normal lines
System.out.print(" ");
}
}
System.out.println();
line++;
}
}
public static void main(String[] args) {
new Test().sizeOfTri(4);
}
}
I commented on next to if statements on which part does what. This will produce an output which looks like the below when run
* *
** **
*** ***
********
Although, all the above implementations are really good ones. I thought of doing it bit differently and make use of 2-D arrays.
package algorithms;
public class DrawMirror {
static void initialize(String[][] array){
for (int i=0; i<MAX_X; i++){
for (int j=0; j < MAX_Y; j++){
array[i][j] = " ";
}
}
}
static void draw(String[][] array, int x, int y){
for (int i=0; i < y; i++){
array[x][i] = "*";
array[x][MAX_Y-i-1] = "*";
}
}
final static int MAX_X = 4;
final static int MAX_Y = 8;
public static void main(String[] args) {
String[][] array = new String[MAX_X][MAX_Y];
initialize(array);
for (int i=0; i < MAX_X ; i++){
draw(array,i,i+1);
}
for( int i = 0; i < MAX_X; i++ ){
for( int j = 0; j < MAX_Y; j++ ){
System.out.print(array[i][j]);
}
System.out.println();
}
}
}
The following code is a function with variable height.
public static void printDoubleTriangle(int height) {
for(int i = 0; i < height; i++) {
for(int j = 0; j < 2*height; j++) {
if(j <= i || (2*height - j - 1) <= i) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}

How do I print this pattern in java?

I'm not able to print this pattern:-
90
890
7890
67890
567890
4567890
34567890
234567890
1234567890
I,ve already tried this:
class loop1
{
public static void main(String args[])
{
for(int i=1;i<10;i++)
{
for(int j=9;j<10;j++)
{ System.out.print(j);
j--;
}
System.out.println("");
}
}
}
but get an output of unlimited 9, a never ending loop.
try this
public static void main(String args[]) {
for (int i = 1; i < 10; i++) {
for (int j = i; j > 0; j--) {
System.out.print(10 - j);
}
System.out.println(0);
}
}
Try this its very simple
working example is here
StringBuffer s = new StringBuffer("0");
for(int i = 9; i > 0; i--) {
s.insert(0, i);
System.out.println(s);
Result
90
890
7890
67890
567890
4567890
34567890
234567890
1234567890
for(int i=1;i<10;i++) {
for(int j=10-i;j<10;)
System.out.print(j++);
System.out.println("0");
}
Try this:
StringBuffer s = new StringBuffer("0");
for(int i = 9; i > 0; i--) {
s.append(i);
System.out.println(s);
}
Your outer loop is looping the wrong way; for a working solution, it should be going from 9 down to 1. Then, your inner for loop would be looping up to 9:
for(int i = 9; i > 0; i--) {
for(int j = i; j < 10; j++) {
System.out.print(j);
}
System.out.println(0);
}
The 0 can't be part of the loop because it doesn't fit with the 1-9 pattern. However, you could print the last digit of each number and cut the 0 part of the println statement:
for(int i = 9; i > 0; i--) {
for(int j = i; j < 10; j++) {
String number = j + "";
System.out.print(number.substring(number.length() - 1));
}
System.out.println();
}
Maybe i am late but i have to post this because of my love for loops
public class SeriesLoop {
public static void main(String a[]){
for(int i=9;i>=1;i--){
for(int j=i;j<=10;j++){
System.out.print(j%10);
}
System.out.println();
}
}
}
DEMO
Cheers

Categories

Resources