Hub Of Geekz

  • Home
  • About Me
  • Contact Us
  • Home
  • Languages
    • C++
    • Java
    • Perl
    • Prolog
    • Bootstrap
  • Database
    • SQL
    • PL/SQL
  • Study
    • Java
      • Java Collection
      • Java Concurrency
      • Java Interview Questions
      • Java Networking
    • Number System
  • Kavita
  • Entertainment
    • Hinglish Cafe
    • Videos
    • Jokes
  • Windows Tricks
  • How To

Monday, 2 March 2015

Star Pattern printing in Java

 Earthcare Foundation NGO     08:04     Java, Java Interview Questions     No comments   

In this post i will explain about different patterns using java program.
1)Our first pattern is
 * 

 *  * 

 *  *  * 

 *  *  *  * 

 *  *  *  *  * 
So how can we print this pattern using Java program.First observe this pattern it has five rows and columns are increasing in each row and max columns are 5 which are equal to number of rows.so for first row we have to print star one time, for row two, two time and so on.
SO how can we do it?
So we have to do two things first we have to use a loop which will iterate for 5 times.
for(int i=1;i<=5;i++)
So above statement will iterate for 5 times next we have to print stars starting from 1 and for each iteration we have to increment it by 1 and the terminating condition is when it reaches value of row number i.e. for row 1 its 1
for row 2 its 2
for row 3 its 3 and so on.
So we can use  for(int j=0;j<i;j++)
So in this way the given pattern will be printed.Below is complete program:
package examples;

public class Pattern {

public static void main(String[] args) {

for(int i=1;i<=5;i++){
for(int j=0;j<i;j++){
System.out.print(" * ");
}
System.out.println("\n");
}

}

}

Output:
 *

 *  *

 *  *  *

 *  *  *  *

 *  *  *  *  *

2)Now we will print following pattern
                                             
                      *
                   * * *
                 * * * * *
              * * * * * * *
            * * * * * * * * *
              * * * * * * *
                 * * * * *
                   * * *
                      *


Program Code:
package examples;

public class Pattern1 {

public static void main(String[] args) {

int n=5;

//for iterating the rows
for (int i = 1; i <= n; i++)

         {
//for printing number of spaces before printing *
               for (int j = 0; j < (n - i); j++)

                     System.out.print(" ");
               //for printing * till half part i.e.
               //   *
             //  * *
           // * * *
        // * * * *
     // * * * * *
               for (int l = 1; l <= i; l++)

                     System.out.print("*");
               //for printing rest of the part
               for (int k = 1; k < i; k++)

                     System.out.print("*");

               System.out.println();

         }


//for printing lower part in same way as upper part is printed
         for (int i = n - 1; i >= 1; i--)

         {

               for (int j = 0; j < (n - i); j++)

                     System.out.print(" ");

               for (int l = 1; l <= i; l++)

                     System.out.print("*");

               for (int k = 1; k < i; k++)

                     System.out.print("*");

               System.out.println();

         }



         System.out.println();
}

}
Output:

                       *
                    * * *
                 * * * * *
              * * * * * * *
           * * * * * * * * *
              * * * * * * *
                 * * * * *
                    * * *
                       *



So above are  some patterns which can be printed using Java program.
Note:The 2nd pattern is not formatting well after coming on this page.
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Ad


Jobsmag.inIndian Education BlogThingsGuide

Subscribe

Do you want fresh and hot updates about us? Then subscribe to our Feeds.

Total Pageviews

Popular Posts

  • Write a program in PL/SQL to print the factorial of a number.
    In this post I will explain how to get the factorial of any given number. For that first you need to know what is the procedure to find ...
  • To find the GCD of two numbers in PROLOG.
    gcd(X,Y):-X=Y,write('GCD of two numbers is '),write(X); X=0,write('GCD of two numbers is '),write(Y); Y=0,write('G...
  • Write a PL/SQL code to get the Fibonacci Sequence
    First, I will explain what is Fibonacci Sequence and how to get this series. So, Fibonacci Sequence is a series of numbers 0,1,1,2,3,5,8,1...

Label

All Articles Best Resources Blogging Boost Traffic Bootstrap C Plus Plus Collection Comedy Comedy Posts Comedy Videos Concurrency creative commons website Education Employee Entertainment Fibonacci Sequence free images GirlFriend Hinglish Cafe How To Image Websites Inspirational Java Java Interview Questions Java Networking Kavita Sangrah Life Lock Sreen Love Number System Patterns Perl Picture PL/SQL Plastic Engineering Programming Prolog public domain SEO Servlet Short Story Shortcut Keys Social Media Social Services SQL SuVichar Thread Traffic True Events Ultimate Guide Windows Tricks Windows8.1 WordPress

Blog Archive

  • ►  2020 (43)
    • ►  September (41)
    • ►  August (2)
  • ►  2019 (1)
    • ►  July (1)
  • ►  2018 (9)
    • ►  September (7)
    • ►  July (1)
    • ►  May (1)
  • ►  2017 (8)
    • ►  June (3)
    • ►  May (3)
    • ►  March (1)
    • ►  January (1)
  • ►  2016 (2)
    • ►  September (1)
    • ►  January (1)
  • ▼  2015 (91)
    • ►  December (1)
    • ►  November (1)
    • ►  October (6)
    • ►  May (10)
    • ▼  March (20)
      • ArrayList Methods in Java
      • ArrayList Methods in Java
      • ArrayList in Java
      • Arrays Class in Java
      • Java Interview Question-Variable length arguments ...
      • Array in Java
      • Data Hierarchy in Operating Systems
      • Synchronization in Java
      • Typecasting in Java
      • Concurrency in Java
      • Runnable in Java
      • HashMap in Java
      • Reverse of a given number using Java
      • Star Pattern printing in Java
      • Difference between fail-fast and fail-safe iterators
      • Factorial of a number in Java
      • Null in Java
      • Final in Java
      • Immutable Objects in Java
      • Thread concept in java
    • ►  February (50)
    • ►  January (3)
  • ►  2014 (339)
    • ►  December (1)
    • ►  October (55)
    • ►  September (58)
    • ►  August (94)
    • ►  July (64)
    • ►  June (67)
  • ►  2013 (34)
    • ►  August (5)
    • ►  April (29)
  • ►  2012 (20)
    • ►  November (1)
    • ►  October (15)
    • ►  September (4)

Author

  • Earthcare Foundation NGO
  • Kavita house
  • Unknown
  • Unknown

Info

Copyright © Hub Of Geekz | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com | Distributed By Gooyaabi Templates