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
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:
- Took n as input.
- printed the first half (1,12,123 and so one) with loop.
- 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:
Similar questions
Math,
2 months ago
English,
2 months ago
CBSE BOARD X,
4 months ago
Science,
4 months ago
Math,
10 months ago