Plz write a code in java to print the following pattern. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Plz write a code in java to print the following pattern.

1 2 6 3 7 10 4 8 11 13 5 9 12 14 15

27th Aug 2017, 10:09 AM
Gammaburst
Gammaburst - avatar
10 Answers
+ 3
Here it is bro! public class floyd_of_triangle { public static void main(String[] args) { for(int x=1;x<=5;x++){ int a=0,b=4; for(int y=1;y<=x; y++){ int s=x+a; System .out.print (s+" "); a=a+b; b--; } System .out .println (); } } }
27th Aug 2017, 11:42 AM
Java Guy
Java Guy - avatar
+ 4
Thanks Java Guy and Baptiste☺ You guys really helped me.
27th Aug 2017, 11:54 AM
Gammaburst
Gammaburst - avatar
+ 1
Note that your request is lacking a few points: - What is the input or behaviour you want? We provide the number of lines to generate? Should be generated always a triangular output? You see... We could, for example, generate this output with a bunch of calls to 'println's...
27th Aug 2017, 10:47 AM
Carlos J Sousa
Carlos J Sousa - avatar
+ 1
Hi Carlos, I don't want any input. The program should make use of "for" loop. Nested loops are allowed and yes output should be exactly the same.
27th Aug 2017, 11:12 AM
Gammaburst
Gammaburst - avatar
+ 1
The for loop syntax is nearly the same in C++ and Java : https://code.sololearn.com/cwiTc3Qa4YxM/?ref=app
27th Aug 2017, 11:19 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
😊
27th Aug 2017, 11:55 AM
Java Guy
Java Guy - avatar
27th Aug 2017, 3:39 PM
Carlos J Sousa
Carlos J Sousa - avatar
0
import java.util.*; class pattern { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("enter n"); int n = sc.nextInt(); { for(int i=0;i<n;i++) { for(int j=0;j<=i;j++) { System.out.print((((2*n-1-j)*j)/2)+(i+1)); } System.out.println(""); } } } }
27th Jan 2018, 5:31 PM
Ratan Kanade
Ratan Kanade - avatar
0
thanks
2nd Aug 2018, 2:05 PM
Pavithra Botlagunta
Pavithra Botlagunta - avatar
0
class DemoTriangle { public static void main(String[] args) { int n=5; for (int i=0; i<n;i++ ) { for (int j=0;j<n ;j++ ) { if (j<=i) { System.out.print((((2*n-1-j)*j)/2)+(i+1)+" "); } } System.out.println(); } } }
12th Apr 2020, 3:40 PM
Ankit Kumar
Ankit Kumar - avatar