brilliant_moves

OneToTenPattern.java

May 21st, 2015
461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.84 KB | None | 0 0
  1. public class OneToTenPattern {
  2.  
  3.     /**
  4.     *   Program:    OneToTenPattern.java
  5.     *   Purpose:    Alternative algorithm for creating number pattern (single for loop).
  6.     *   Creator:    Chris Clarke
  7.     *   Created:    21.05.2015
  8.     */
  9.  
  10.     /*
  11.  
  12.         Java program to create the following pattern:
  13.  
  14.         1
  15.         23
  16.         456
  17.         78910
  18.  
  19.     */
  20.  
  21.     public static void main(String[] args) {
  22.  
  23.         int perLine=1;  // number of numbers per line
  24.         int count=0;    // numbers printed in current line
  25.  
  26.         for (int n=1; n<=10; n++) {     // n loops from 1 to 10
  27.             System.out.print(n);        // print n
  28.             count++;            // increment counter
  29.             if (count==perLine) {       // if we've printed the right number of numbers for this line
  30.                 System.out.println();   // new line
  31.                 perLine++;      // increment numbers per line
  32.                 count=0;        // reset counter
  33.             } // if
  34.         } // for
  35.  
  36.     } // main()
  37.  
  38. } // class OneToTenPattern
Advertisement
Add Comment
Please, Sign In to add comment