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 the difference between preparedstatement and statement in java?

1 Answers  


Can we use String with switch case?

1 Answers  


Does java initialize arrays to zero?

1 Answers  


What does jenkins do?

1 Answers  


How do I write a self declaration?

1 Answers  


What is the differnence between String Buffer and String builder despite having knowledge that String builder is faster than String Buffer and last one is threadsafe.please tell another important difference.

3 Answers   IBM,


What is low level language in computer?

1 Answers  


can we access the method of class without creating the object of the class

3 Answers  


The class "Class" is belongs to which package?? a) java.lang b)java.lang.reflect c)java.util d)None

4 Answers  


What are listeners in java and explain ?

2 Answers   TCS,


Is int a class in java?

1 Answers  


What are the differences between abstract class and interface?

1 Answers  


Categories