Java Programs


Program to print  upper half diamond.

class h
{
           
            public static void main(String ar[])
            {
                        int i,j,k;
                        for(i=1;i<=5;i++)
                        {
                                    int c=1;
                                    for(j=4;j>=i;j--)
                                    {
                                                System.out.print(" ");
                                    }         
                                    for(k=1;k<=i;k++)
                                    {
                                                System.out.print("*");
                                                System.out.print(" ");
                                               
                                    }
                                    System.out.print("\n");
                        }
            }
}




Program to print    1
                                  1 2
                                  1 2 3
                                  1 2 3 4

class i
{
public static void main(String ar[])
             {
                        int i,j;
                        for(i=1;i<=5;i++)
                        {
                                    for(j=1;j<=i;j++)
                                    {
                                                System.out.print(j);
                                    }         
                                    System.out.print("\n");
                        }
}
}



Program to print                1
                                           1   2
                                         1   2  3
                                       1  2   3   4

class j
{
            public static void main(String ar[])
            {
                        int i,j,k;
                        for(i=1;i<=5;i++)
                        {
                                    int c=1;
                                    for(j=4;j>=i;j--)
                                    {
                                                System.out.print(" ");
                                    }         
                        for(k=1;k<=i;k++)
                                    {
                                                System.out.print(k);
                                                System.out.print(" ");
                                               
                        }
                                    System.out.print("\n");
                        }
            }
}


Program to print    1 2 3 4
                                   1 2 3
                                      1 2
                                         1

class k
{
             public static void main(String ar[])
            {
                        int i,j,k;
                        for(i=4;i>0;i--)
                        {
                                    for(j=4;j>i;j--)
                                    {
                                                System.out.print(" ");
                                    }         
                                    for(k=1;k<=i;k++)
                                    {
                                                System.out.print(k);
                                    }
                                     System.out.print("\n");
                        }
            }
           
}


Program to print               1
                                         1 0
                                        1 0 1
                                       1 0 1 0
                                      1 0 1 0  1
class m
{
            public static void main(String ar[])
            {
                        int i,j,k;
                        for(i=1;i<=5;i++)
                        {
                                    int c=1;
                                    for(j=4;j>=i;j--)
                                    {
                                                System.out.print(" ");
                                    }         
                                    for(k=1;k<=i;k++)
                                    {
                                                System.out.print(c);
                                                System.out.print(" ");
                                                if(c==1)
                                                {
                                                            c=0;    
                                                }
                                                else
                                                {
                                                            c=1;
                                                }
                        }
                                    System.out.print("\n");
                        }
            }
}