Computer Science, asked by TreeshaBiswas, 4 months ago

print this pattern in java using for loop:-
1
121
12321
1234321


BrainlyProgrammer: is this pattern in pyramid form or triangular form??

Answers

Answered by allysia
62

Program:

import java.util.Scanner;

public class oh { public static void main(String[] args)

{ Scanner scan= new Scanner (System.in);

System.out.print("Enter the highest number: ");

 int n=scan.nextInt();

  for (int i = 1; i < n+1; i++) {

for (int k=1; k<i+1; k++) {System.out.print(k);}

for (int k=i-1; k>0; k-=1)  { System.out.print(k);}

System.out.println();}  

} }

Explanation for what I did:

  1. Took n as input.
  2. printed the first half (1,12,123 and so one) with loop.
  3. combined with with other half with other loop and print method (which includes 1, 21, 321 from reversed side, play around with the loop and you'll understand what I'm saying)

NOTE: Consider the attachments for output and program in IDE.

Attachments:

TreeshaBiswas: ohhh
TreeshaBiswas: so could you please do in PYRAMID FORM only which u had done before.... actually by mistake I gave this one instead of the pyramid one
allysia: It's okay I already posted the answer.
viratdhoni187: nice explanation
BrainlyProgrammer: @allysia Gr8 Answer!!!
Anonymous: Nice ✌
anindyaadhikari13: Superb.
Anonymous: Noiceee !!!
iTzShInNy: Superb!! :)
Lovelycandy: Keep it up :)
Similar questions