+ 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
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 ();
}
}
}
+ 4
Thanks Java Guy and Baptisteâș You guys really helped me.
+ 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...
+ 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.
+ 1
The for loop syntax is nearly the same in C++ and Java :
https://code.sololearn.com/cwiTc3Qa4YxM/?ref=app
+ 1
đ
+ 1
Well, that's my try...
https://code.sololearn.com/c5T3a5qEkxya/?ref=app
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("");
}
}
}
}
0
thanks
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();
}
}
}