Coding using for loop in Java
X
X X X
X X X X X
X X X X X X X
Answers
Answered by
1
Answer:
public class Star {
public static void main(String[] args) {
int i,j;
for (i=1; i<=5; i++)
{
for (j=1; j<=(2*i-1); j++)
{
System.out.print(" X");
}
System.out.println();
}
}
}
Explanation:
We have to print X in odd number that's why we have taken (2*y-1) in second for loop so when i=1 then its answer will be 1 so that will print X once and then next number will be i = 2 so the anwer will be 3 so The X will be printed 3 times and so on.
Answered by
0
Answer
public class Star {
public static void main(String[] args) {
int i,j;
for (i=1; i<=5; i++)
{
for (j=1; j<=(2*i-1); j++)
{
System.out.print(" X");
}
System.out.println();
}
}
}
Explanation
We have to print X in odd number that's why we have taken (2*y-1) in second for loop so when i=1 then its answer will be 1 so that will print X once and then the next number will be i = 2 so the answer will be 3 so The X will be printed 3 times and so on.
Similar questions