Write a program to print 1 121 12321 1234321 12321 121 1 in rhombus form in java
Answers
Answered by
8
Hey there ☺
Here is your solution -:
_____________________
{This answer is just an example but you have to do the same as done but put the exact number in the appropriate place}
// Java program to print rhombus pattern
import java.util.Scanner;
class Pattern {
void display(int n)
{
// sp stands for space
// st stands for number
int sp = n / 2, st = 1;
// Outer for loop for number of lines
for (int i = 1; i <= n; i++) {
// Inner for loop for printing space
for (int j = 1; j <= sp; j++)
System.out.print(" ");
// Inner for loop for printing number
int count = st / 2 + 1;
for (int k = 1; k <= st; k++) {
System.out.print(count);
if (k <= st / 2)
count--;
else
count++;
}
// To goto next line
System.out.println();
if (i <= n / 2) {
// sp decreased by 1
// st increased by 2
sp = sp - 1;
st = st + 2;
}
else {
// sp increased by 1
// st decreased by 2
sp = sp + 1;
st = st - 2;
}
}
}
// Driver code
public static void main(String[] args)
{
Pattern p = new Pattern();
int n = 7;
p.display(n);
}
}
OUTPUT
1
212
32123
4321234
32123
212
1
______________________
Hope it helps :)
Here is your solution -:
_____________________
{This answer is just an example but you have to do the same as done but put the exact number in the appropriate place}
// Java program to print rhombus pattern
import java.util.Scanner;
class Pattern {
void display(int n)
{
// sp stands for space
// st stands for number
int sp = n / 2, st = 1;
// Outer for loop for number of lines
for (int i = 1; i <= n; i++) {
// Inner for loop for printing space
for (int j = 1; j <= sp; j++)
System.out.print(" ");
// Inner for loop for printing number
int count = st / 2 + 1;
for (int k = 1; k <= st; k++) {
System.out.print(count);
if (k <= st / 2)
count--;
else
count++;
}
// To goto next line
System.out.println();
if (i <= n / 2) {
// sp decreased by 1
// st increased by 2
sp = sp - 1;
st = st + 2;
}
else {
// sp increased by 1
// st decreased by 2
sp = sp + 1;
st = st - 2;
}
}
}
// Driver code
public static void main(String[] args)
{
Pattern p = new Pattern();
int n = 7;
p.display(n);
}
}
OUTPUT
1
212
32123
4321234
32123
212
1
______________________
Hope it helps :)
jtydcsm:
I asked for java
Similar questions