Write Java programs to print the following patterns. Do not use nested loops.
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
---------------------------------------------------------
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
--------------------------------------------------------
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
A
B B
C C C
D D D D
E E E E E
F F F F F F
---------------------------------------------------------
*****
****
***
**
*
---------------------------------------------------------
*
**
***
****
*****
******
---------------------------------------------------------
1
10
101
1010
10101
Write the code in java
Plzz help its urgent
Answers
Answer:
1)
Explanation:
public class Pattern {
public static void main(String[] args) {
int rows = 5;
for(int i = 1; i <= rows; ++i) {
for(int j = 1; j <= i; ++j) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
Answer:
public class pattern_2
{
void main()
{
int i, j, p=1;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(p+" ");
p++;
}
System.out.println();
}
}
}
import java.util.*;
public class pattern_3
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int rows = sc.nextInt();
for (int i = rows; i >= 1; i--)
{
for (int j = rows; j >= i; j--)
{
System.out.print(j+" ");
}
System.out.println();
}
sc.close();
}
}
import java.util.*;
public class pattern_4
{
public static void main(String args[])
{
int alphabet = 65;
for (int i = 0; i<= 5; i++)
{
for (int j = 0; j <= i; j++)
{
System.out.print((char) alphabet + " ");
}
alphabet++;
System.out.println();
}
}
}
import java.util.*;
public class pattern_5
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of rows: ");
int rows = sc.nextInt();
for (int i= rows; i>= 1; i--)
{
for (int j=rows; j>i;j--)
{
System.out.print(" ");
}
for (int k=1;k<=i;k++)
{
System.out.print("*");
}
System.out.println("");
}
sc.close();
}
}
import java.util.*;
public class pattern_6
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of rows: ");
int rows = sc.nextInt();
for (int i= 0; i<= rows; i++)
{
for (int j=1; j<=rows-i; j++)
{
System.out.print(" ");
}
for (int k=0;k<=i;k++)
{
System.out.print("*");
}
System.out.println("");
}
sc.close();
}
}
import java.util.*;
public class pattern_7
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int rows = sc.nextInt();
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
if(j%2 == 0)
{
System.out.print(0);
}
else
{
System.out.print(1);
}
}
System.out.println();
}
sc.close();
}
}