Tuesday, October 25, 2011

Printing a pyramid pattern of digits #8

//This java program accepts a number of user
//which tells the program that how many lines
//of the pattern will be printed and then prints
//the pattern of the given number of lines.
/*Sample Input : 6

Sample Output :
6
656
65456
6543456
654323456
65432123456 */
//For a center aligned version of this pattern
//visit : Java Programs: Printing a pyramid pattern of digits 9 (Java)

import java.util.*;
class pattern
{
public static void main()
{
    System.out.print("Enter the number of lines you want : ");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int i,j,k=n,l=1;
    for(i=1;i<=n;i++)
    {
        k=n;
        for(j=1;j<=i;j++,k--)
        {
            System.out.print(k);
        }
        k=k+2;
        for(j=1;j<=i-1;j++,k++)
        {
            System.out.print(k);
        }
        System.out.println();
    }
}
}
//Author : Mayank Rajoria

No comments:

Post a Comment