Java program to print this pattern
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Answers
Number pattern - Java
Before we start programming the Number pattern, let's first know about that what is Java and what is programming.
Java is a high level programming language, it was developed by Sun Microsystems in the early early 1990s, it was designed by different platforms.
Programming means to create or develop a software which is called a program that contains the instructions to tell a computer what to do.
The Program
class NumberPattern
{
public static void main(String arg[])
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}
The Output
The output of the given program will be as follows:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Required Answer.
Approach 1: Using 2 loop
class patt{
public static void main (String ar []){
for(int I=1;I<=5;I++){
for(int j=1;j<=I;j++)
System.out.print(j+" ");
System.out.println();
}
}
}
Approach 2: Using 1 loop
class patt{
public static void main (String ar []){
int c=0;
for(int I=1;I<=5;I++){
c=c*10+I;
System.out.println(c);
}
}
}
Approach 3: using Integer.toString()
- Similar with approach 2
class NumberPat1
{
public static void main(String arg[])
{
String k="";
for (int i = 1; i <= 5; i++)
{
k+=Integer.toString(i)+" ";
System.out.println(k);
}
}
}