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

Saturday, 10 October 2015

Java program to print patterns

 Unknown     00:17     Java, Patterns, Programming     No comments   

In this post I will explain the logic of printing different types of patterns. This post will clear all doubts for printing patterns and after this post you can print any type of patterns.
The most important thing about printing patterns is the logic, the way it can be printed, we just have to learn that logic after we get the idea of logic, we can print it using any programming language.
For this post I am using Java language, but as I said logic is important, if you are able to grab that logic then you can use any programming language to print those patterns.
First of all try to think a rectangular space for printing patterns which can be divided to 4 parts, following image shows that division:


So in above figure we have 4 parts, so while we will be printing something we will take these areas into consideration. For printing those parts we will be using for loop for each part.
For example suppose if we are going to print the following pattern
1
12
123
1234
12345
Then we are concerned about only first part.
And if we are going to print the following pattern

Then we are concerned about all 4 parts
In first part we have to print that number part after that in second and third part we have to print spacing part and in fourth part we again have to print that number part.
So in this way we will be printing the patterns.
Let’s print our first pattern which is

1                                       (1)
12                                     (2)
123                                   (3)
1234                                 (4)
12345                               (5)


For printing patterns first thing we need to know the number of lines we are going to print. In above pattern we have 5 lines so we will be using a for loop for tracking those lines.
·         For(int i=1,i<=5;i++)

After that since in above pattern we are concerned about first part only so we need only one for loop.
Now for first time when the outer loop execute then at that time the value of i would be 1 and we also have to print 1 in first line , for second time we have to print 1 and 2 and at that time value of I would be 2. So I think we can create some sequence. Suppose for second loop we will be using variable j then sequence is
·         For first row I=1    ==> we have to print j=1
·         For second row I=2    ==> we have to print j=1 2
·         For third row I=3    ==> we have to print j=1 2 3
·         For fourth row I=4    ==> we have to print j=1 2 3 4
·         For fifth row I=5    ==> we have to print j=1 2 3 4 5

That’s it 
We have created some type of relationship, for each row we have to print the value of j which will start from 1 and go till the value of i.
·                     for(int j=1;j<=i;j++){
·                      System.out.print(j);

And after completing 1 inner loop for value of j we have to change the line for that we will use this statement
·         System.out.print();
So in this way the above pattern will be printed.
Program for that pattern is following:



Program Code:
public class SimplePattern {


      public static void main( String arg[]){
                     //for row counts
                      for(int i=1;i<=5;i++){
                          //for printing numbers
                            for(int j=1;j<=i;j++){
                                System.out.print(j);
                            }
                           //for entering to new line
                            System.out.println();
                        }
                    }
                }


Output:


1
12
123
1234
12345
  


Now we are done with our first pattern we will print our next pattern which is following:




So in above pattern how any loops will be required????
·         1 for row count
·         4 for printing 4 parts
·         Total 5 loops
·         Outer Loop : for(int i=1;i<=5;i++)   Since 5 rows are there
·          For 1st part for(int j=1;j<=i;j++){
·              System.out.print(j);
First part I have already explained how to print.
So now I will explain second part, for this part we will be using variable k

Let’s try to establish some relationship
·         Initially we will need 4 spaces, in second row we will need 3 spaces for next row we will need 2 and so on
·         For this we will take one variable in outer loop and we will initialize its value with 4 and decrement it.
·         Our modified outer loop for(int i=1,z=4;i<=5;i++,z--)
·         Our for loop for second part is for(int k=z;k>=1;k--){
·                                              System.out.print(" ");
·                                             }
                                                               

Now we will try to print our 3rd part which is also equivalent to our second part so we will need same for loop values
·         for(int l=z;l>=1;l--){
·         System.out.print(" ");   
·         }

That’s it now we need our 4th part in which we have to print the values just like first part. So for loop for this part will be like 1st part
·         for(int m=1;m<=i;m++){
·         System.out.print(m);    
·         }


So that’s it we are done.
Following is the program to print the above pattern


Program Code:

public class SimplePattern {

                public static void main(String[] args) {
                                // TODO Auto-generated method stub

                                                for(int i=1,z=4;i<=5;i++,z--){
                                                                for(int j=1;j<=i;j++){
                                                                                System.out.print(j);       
                                                                }
                                                                for(int k=z;k>=1;k--){
                                                                                System.out.print(" ");   
                                                                }
                                                                for(int l=z;l>=1;l--){
                                                                                System.out.print(" ");   
                                                                }
                                                                for(int m=1;m<=i;m++){
                                                                                System.out.print(m);    
                                                                }
                                                                System.out.println();    
                                }
}







We have also seen patterns which will be towards downside direction like


12345
1234
123
12
1
And many more same logic can be applied there too.
So I am showing only program an output if there is any confusion or if you are not able to understand then I will also explain those patterns.



 
Program Code:


public class SimplePattern {

                public static void main(String[] args) {
                                // TODO Auto-generated method stub

                                for(int i=5;i>=1;i--){

                                   for(int j=1;j<=i;j++){
                                        System.out.print(j);
                                   }
                                 System.out.println();
                             }
                }

}



 Output:

12345
1234
123
12
1


 
Program Code:


public class SimplePattern {

                public static void main(String[] args) {
                                // TODO Auto-generated method stub

                                                for(int i=1;i<=5;i++){
                                                                for(int j=1;j<=i;j++){
                                                                                System.out.print(j);       
                                                                }
                                                                System.out.println();    
                                }
                               
                                                for(int i=4;i>=1;i--){

                                                                for(int j=1;j<=i;j++){
                                                                                System.out.print(j);
                                                                }
                                                                System.out.println();
                                                }
                               
                }

}


 Output:

1
12
123
1234
12345
1234
123
12

1








Program Code: 


public class SimplePattern {

                public static void main(String[] args) {
                                // TODO Auto-generated method stub

                                                for(int i=1,z=4;i<=5;i++,z--){
                                                                for(int j=1;j<=i;j++){
                                                                                System.out.print(j);       
                                                                }
                                                                for(int k=z;k>=1;k--){
                                                                                System.out.print(" ");   
                                                                }
                                                                for(int l=z;l>=1;l--){
                                                                                System.out.print(" ");   
                                                                }
                                                                for(int m=1;m<=i;m++){
                                                                                System.out.print(m);    
                                                                }
                                                                System.out.println();    
                                }
                               
                                                for(int i=4,z=1;i>=1;i--,z++){

                                                                for(int j=1;j<=i;j++){
                                                                                System.out.print(j);
                                                                }
                                                                for(int j=1;j<=z;j++){
                                                                                System.out.print(" ");
                                                                }
                                                                for(int j=1;j<=z;j++){
                                                                                System.out.print(" ");
                                                                }
                                                                for(int m=1;m<=i;m++){
                                                                                System.out.print(m);
                                                                }
                                                                System.out.println();



                                }
                               
                }
}                



Output:





So above is all about patterns if there is any confusion or you want to print some other patterns please comment. I will try to explain anything you want.
Thanks for reading this post.





  • 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)
      • Top 5 free and best blogging platforms
      • WordPress:How to create a free blog in 15 minutes
      • Great Topics for a Blog
      • PL/SQL Tutorial
      • Why String is immutable in Java
      • Java program to print patterns
    • ►  May (10)
    • ►  March (20)
    • ►  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