Wednesday, 26 February 2014

Program to print Triangle Patterns in Java



1. Prefect Triangle Program in Java


package numberpatterns;

/**
 *
 * @author Karan
 */
public class StarTriangle {

    public static void main(String[] args) 
    {
        int num = 10;
        for(int i=1;i<=num;i++)
        {
            for(int j=1;j<num-(i-1);j++)
            {
                System.out.print(" ");
            }
            for(int k=1;k<=i;k++)
            {
                System.out.print("*");
                for(int k1=1;k1<k;k1+=k)
                {
                    System.out.print("*");
                }
            }
            
            System.out.println();
        }
    }
}

Output:

triangle pattern in java


2. Upside Down triangle Program in Java



package numberpatterns;

public class ReverseStarTriangle 
{

    public static void main(String[] args)
    {
        int num = 10;
        for(int i = 1; i <= num; i++)
        {
            for(int j=i;j>1;j--)
            {
                System.out.print(" ");
            }
            for(int k = 1; k <= num - (i-1); k++)
            {
                System.out.print("*");
                for(int k1 = 1; k1 < k; k1+=k)
                {
                    System.out.print("*");
                }
            }
            System.out.println();          
        }
    }
}

Output : 


traingle pattern java




Get Ebooks delivered to your email id

Comments

Subscribe to our channel

Facebook

Powered by Blogger.

Home | Contact Us | DMCA | Terms of Service | Privacy | Advertise

Maven Scientists