Please solve the program
Answers
Required Answer:-
Question:
Write a Java program to display the following pattern.
8 8 8 8
6 6 6
4 4
2
Solution:
This is an easy question. Here is the code.
public class Pattern {
public static void main(String[] args) {
for(int i=8;i>=2;i-=2)
{
for(int j=2;j<=i;j+=2)
System.out.print(i+" ");
System.out.println();
}
}
}
This can be solved using 1 loop too!! Here is the code.
public class Pattern {
public static void main(String[] args) {
int j=2;
for (int i=8;i>=2;)
{
if(j<=i)
{
System.out.print(i+" ");
j+=2;
}
else
{
i-=2;
j=2;
System.out.println();
}
}
}
}
Output is attached.
//This is done in JAVA
package Programmer;
import java.util.*;
public class Pattern3{
public static void main (String ar []) {
for(int I=8;I>=1;I-=2) {
for(int j=2;j<=I;j+=2)
System.out.print(I+" ");
System.out.println();
}
}
}
_______
Variable Description:-
- I:- loop variable
- j:- loop variable