What's new

Help Patulong po assignment lang Java language

Schiro

Forum Veteran
Joined
Jul 11, 2018
Posts
1,675
Solutions
4
Reaction
1,579
Points
629
Kailangan po magcreate ng left triangle java program na ang output is:
1
1 2
2 3 4
4 5 6 7
7 8 9 10 11

Salamat po sa makakatulong
 
Hello. I separated the first line of output sa loop because I can't seem to think na ngayon. If you want me to include the first output with the loop, tell me.

class Main {
public static void main(String []args) {
int n = 1; int f = 0; int r = 1;

System.out.println("1");
for (int z = 1; z <= 4; z++) {
n+=f;
System.out.print(n + " ");
f++;
for (int x = 0; x < z; x++) {
r++;
System.out.print(r + " ");


}
System.out.println();

}
}
}

Or you can use this instead,

public class Main {
public static void main(String[] args) {
int num = 1;
for(int i = 1; i < 6; i++) {
for(int j = 1; j <= i; j++) {
System.out.print(num + " ");
num++;
}

System.out.println();
num--;

}
}
}
 
Last edited:
Hello. I separated the first line of output sa loop because I can't seem to think na ngayon. If you want me to include the first output with the loop, tell me.

class Main {
public static void main(String []args) {
int n = 1; int f = 0; int r = 1;

System.out.println("1");
for (int z = 1; z <= 4; z++) {
n+=f;
System.out.print(n + " ");
f++;
for (int x = 0; x < z; x++) {
r++;
System.out.print(r + " ");


}
System.out.println();

}
}
}

Or you can use this instead,

public class Main {
public static void main(String[] args) {
int num = 1;
for(int i = 1; i < 6; i++) {
for(int j = 1; j <= i; j++) {
System.out.print(num + " ");
num++;
}

System.out.println();
num--;

}
}
}
Maraming salamat po.
 

Similar threads

Back
Top