print pattern in java
1
3 5
7 9 11
13 15 17 19
21 23 25 27 29
Answers
Answered by
32
class Pattern
{
public static void main( String args [])
{
for (int x = 1; x <5; x++)
{
for (int y = 1; y <= x; y= y+2)
{
System.out.println( y + " ");
}
System.out.println();
}}}
{
public static void main( String args [])
{
for (int x = 1; x <5; x++)
{
for (int y = 1; y <= x; y= y+2)
{
System.out.println( y + " ");
}
System.out.println();
}}}
Pulkit123:
thanks bro
Answered by
1
The code to print the given pattern is as follows:
class Solution {
public static void main (String[] args) {
int k = 1;
for(int i = 1; i <= 5; i++) {
for(int j = 1; j <= i; j++) {
System.out.print(k + " ");
k += 2;
}
System.out.println();
}
}
}
- First, we initialize the variable 'k' with the value 1.
- Then, we run a loop variable 'i' from 1 to 5.
- Then, we run another loop variable 'j' from 1 to i.
- Then we print k and increment k by 2.
- Lastly we take the cursor to the next line using System.out.println().
#SPJ3
Similar questions