Wednesday 21 December 2016

Java Program - Pattern How To Print A Triangle In Java Using For Loop Printing *s as triangles in Java?

Java Program - Pattern 1
public class JavaProgram
{
    public static void main(String args[])
    {
        int i, j;
        for(i=0; i<5; i++)
        {
            for(j=0; j<=i; j++)
            {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}:
Java Program print patterns
Java Program - Pattern 2
public class JavaProgram
{
    public static void main(String args[])
    {
        int i, j, k=1;
        for(i=0; i<5; i++)
        {
            for(j=0; j<k; j++)
            {
                System.out.print("* ");
            }
            k = k + 2;
            System.out.println();
        }
    }
}
:
print pattern in Java Programming
Java Program - Pattern 3
public class JavaProgram
{
    public static void main(String args[])
    {
        int i, j, k=8;
        for(i=0; i<5; i++)
        {
            for(j=0; j<k; j++)
            {
                System.out.print(" ");
            }
            k = k - 2;
            for(j=0; j<=i; j++)
            {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}
Java Program print star pattern
Java Program - Pattern 4
public class JavaProgram
{
    public static void main(String args[])
    {
        int i, j, k=16, tim=1;
        for(i=0; i<5; i++)
        {
            for(j=0; j<k; j++)
            {
                System.out.print(" ");
            }
            k = k - 4;
            for(j=0; j<tim; j++)
            {
                System.out.print("* ");
            }
            tim = tim + 2;
            System.out.println();
        }
    }
}
print star pattern in Java Programming
Java Program - Pattern 5
public class JavaProgram
{
    public static void main(String args[])
    {
        int i, j, n=1;
        for(i=0; i<5; i++)
        {
            for(j=0; j<=i; j++)
            {
                System.out.print(n+ " ");
                n++;
            }
            System.out.println();
        }
    }
}
When the above Java Program is compile and executed, it will produce the following output:
looping in Java Programming
Java Program - Pattern 6
public class JavaProgram
{
    public static void main(String args[])
    {
        int i, j, num;
        for(i=0; i<5; i++)
        {
            num=1;
            for(j=0; j<=i; j++)
            {
                System.out.print(num+ " ");
                num++;
            }
            System.out.println();
        }
    }
}
When the above Java Program is compile and executed, it will produce the following output:
Java Program print pattern using looping
Java Program - Pattern 7
Following Java Program ask to the user to enter the number of rows to print the pyramid of stars:
import java.util.Scanner;

public class JavaProgram
{
    public static void main(String args[])
    {
        int i, space, rows, k=0;
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter Number of Rows : ");
        rows = scan.nextInt();
        for(i=1; i<=rows; i++)
        {
            for(space=1; space<=(rows-i); space++)
            {
                System.out.print("  ");
            }
            while(k != (2*i-1))
            {
                System.out.print("* ");
                k++;
            }
            k = 0;
            System.out.println();
        }
    }
}
When the above Java Program is compile and executed, it will produce the following output:
Java Program print star pyramid



Number Pattern Programs In Java :

Java programs to print the numbers or any other symbols in different patterns are one of the frequently asked interview programs mostly for freshers. Because, they test the candidate’s logical ability as well as coding skills which are ‘must have skills’ for any software engineer. In this post, I have collected some of the different number pattern programs in java and have tried to solve them. I hope they will be helpful for you guys.

No comments:

Post a Comment

Featured post

Life Infotech now a leading brand in the field of technology training

  Life Infotech now a leading brand in the field of technology training & its invites students around the nation to be a part of the Tra...