Sunday, October 16, 2011

Printing a pyramid pattern of digits #4

//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 :4
Sample Output :
1
0 1
1 0 1
0 1 0 1 */

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,c=1,k=-1;
    int a=1,b=-1;
    for(i=1;i<=n;i++)
    {
        c=a;
        k=b;
        for(j=1;j<=i;j++)
        {
            System.out.print(c+" ");
            c=c+k;     
            k=k*(-1);  
        }
        System.out.println();
        a=a+b;     
        b=b*(-1);
    }
}
}
//Author : Mayank Rajoria

3 comments:

  1. Sir i have some problem regarding loop. Sometime i cant understand how loop is working and i cant implement it right too. How can i improve my understanding skill, please help if possible.

    ReplyDelete
  2. Saw this somewhere the other day, Minecraft is awesome though and some of the things people have made... amazing!

    ReplyDelete
  3. printing san diego: You could practice for loop programs right up from the most basic ones. You could also try to understand the logic and working of the for loops on: http://javamylanguage.blogspot.com/search/label/For%20Loop

    ReplyDelete