Program to print 1
1 2
1 2 3
1 2 3 4 like that

Answers were Sorted based on User's Feedback



Program to print 1 1 2 1 2 3 ..

Answer / ravi jain

public class Test
{
public static void main(String[] args)
{
int sp=5;
for(int n=1;n<=5;n++)
{
for(int s=1;s<=sp;s++)
{
System.out.print(" ");
}
for(int i=1; i<=n;i++)
{
System.out.print(i+" ");
}
System.out.println();
sp--;
}
}
}

Is This Answer Correct ?    13 Yes 1 No

Program to print 1 1 2 1 2 3 ..

Answer / rajesh

public class Triangle {

public static void main(String args[])
{
int i,j,k,sp=30;
for(i=1;i<=5;i++)
{
for(k=0;k<sp;k++)
{
System.out.print(" ");
}
sp=sp-1;
for(j=1;j<=i;j++)
{
System.out.print(" "+j);
}
System.out.println();
}
}

}

Is This Answer Correct ?    10 Yes 1 No

Program to print 1 1 2 1 2 3 ..

Answer / yun hin

class triangle{
public static void main(String args[]){
int height = 5;

for(int i=1;i<=height;i++){
for(int j=1;j<=(height-i);j++){
System.out.print(" ");
}
for(int k=1;k<i;k++){
System.out.print(k + " ");
}
System.out.print(i);
System.out.println();
}
}
}

Is This Answer Correct ?    7 Yes 0 No

Program to print 1 1 2 1 2 3 ..

Answer / sunil pradhan

public class program1 {

public static void main(String[] args){

for(int i = 1; i<=5 ;i++){

for(int k = 1; k<=i; k++){

System.out.print(k);
}
System.out.println("\n");
}

}

}

Is This Answer Correct ?    6 Yes 2 No

Program to print 1 1 2 1 2 3 ..

Answer / yagnik

public static void main(String args[]) throws
UnknownHostException, ClassNotFoundException
{
int count =5;

for(int i=1,h=5; i<count;i++){

for(int k=h;k>=0;k--)
{System.out.print(" ");}
for (int j=1 ;j<=i;j++){
System.out.print
(j);System.out.print(" ");
}
h--;System.out.println();
}

}

Is This Answer Correct ?    2 Yes 0 No

Program to print 1 1 2 1 2 3 ..

Answer / vivek

#include<iostream>
using namespace std;
int main()
{
int i,j,n,k;
cout<<"provide the no. of row";
cin>>n;
for(i=1; i<=n; i++)
{
k=n-i;
while(k>0)
{
cout<<" ";
k--;
}
for(j=1; j<i; j++)
cout<<" "<<j<< " ";
cout<<"\n";
}
return(0);
}

Is This Answer Correct ?    1 Yes 0 No

Program to print 1 1 2 1 2 3 ..

Answer / aditya

public class pattern
{
public static void main(int n)
{
int i,j,k;
for(i=1;i<=n;i++)
{
for(j=n;j>i;j--)
{
System.out.print(" ");
}
for(k=1;k<=i;k++)
{
System.out.print(+k+" ");
}

System.out.println();
}
}

Is This Answer Correct ?    0 Yes 1 No

Program to print 1 1 2 1 2 3 ..

Answer / subash chandra bose l

class Tri
{
public static void main(String args[])
{
int i,j,k,sp=30;
for(i=1;i<=5;i++)
{
for(k=0;k<sp;k++)
{
System.out.println(" ");
}
sp=sp-2;
for(j=1;j<=i;j++)
{
System.out.println(" "+j);
}
}
}
}
this will print up to 5 rows and if u want more rows to be
printed you can increase the i<=n where n may be the number
of rows you want to print.

Is This Answer Correct ?    3 Yes 7 No

Post New Answer

More Core Java Interview Questions

What is string variable?

0 Answers  


What is complexity in java?

0 Answers  


What is an object in java and how is it created?

0 Answers  


What is the difference between a Window and a Frame?

6 Answers   Infosys, ProKarma,


what is the difference between cd & dvd ?

30 Answers   Lovely Professional University, Satyam, Sibylsys,






Can You Have Virtual Functions In Java?

0 Answers  


What is use of inner class in java?

0 Answers  


Is break statement can be used as labels in java?

0 Answers  


What is the output of the below java program?

0 Answers  


What is arrays sort in java?

0 Answers  


What is balanced tree in java?

0 Answers  


How do u provide security in java

3 Answers  


Categories