Results 1 to 16 of 16
Like Tree1Likes
  • 1 Post By SurfMan

Thread: Display Patterns use Nested Loops

  1. #1
    LittleJava is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default Display Patterns use Nested Loops

    Hai i am newbie in java , can someone helpe me to get output like this :
    Hoping can show some simple java source code by using nested Loop that is easy to understand. Many Thanks

    1s Pattern:

    1
    2 4
    3 6 9
    4 8 12 16

    2nd Pattern:

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

    3rd Pattern :

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

    4th Pattern:

    *********
    *********
    *********
    *********
    ...

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Display Patterns use Nested Loops

    Just read my signature.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    929
    Rep Power
    2

    Default Re: Display Patterns use Nested Loops

    This cannot be done in Java. Java uses a binary system in powers of two, so odd numbers like 3 and 9 can't be created.

    On a more serious note: ofcourse this can be done, but this is a forum, not a Code Factory™. Show your code and where you run into problems. We're here to help, not to spoonfeed you teh codez of the Jav.
    PhHein likes this.

  4. #4
    LittleJava is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default Re: Display Patterns use Nested Loops

    Quote Originally Posted by SurfMan View Post
    This cannot be done in Java. Java uses a binary system in powers of two, so odd numbers like 3 and 9 can't be created.

    On a more serious note: ofcourse this can be done, but this is a forum, not a Code Factory™. Show your code and where you run into problems. We're here to help, not to spoonfeed you teh codez of the Jav.
    I am so sorry , i am never thought as this forum as Code Factory. Here we go this is my 1st try on the 1st pattern , where i learn that i can put a pattern 1 but as sequence 1 ,2 ,3 ... but how to change make it as multiplier.

    Java Code:
    public class Pattern{
            public static void main(String[]args){
                int i,j,k;
    
             for(i=0; i<4; i++) 
             {
                 for (k=i+1; k<= 2*i+1; k++)
                  {
                    System.out.print( k % 10); 
                  }
             System.out.println();
             }
           }
        }

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    929
    Rep Power
    2

    Default Re: Display Patterns use Nested Loops

    The inner loop is wrong. You multiply by 2, but you need to multiply by i+1. Your code would be a bit more readable if you run your outer loop from 1: for( int i = 1; i <=4; i++ ). Now you can see exactly what its bounds are without having to resort to i+1 all the time. But again, that's merely my personal preference.

    Here's the loop in calculated form:
    Java Code:
    (1*1)
    (1*2) (2*2)
    (1*3) (2*3) (3*3)
    (1*4) (2*4) (3*4) (4*4)
    See the pattern?

    You also don't need the % operator. If your loops are correct, you only need to print i*k.

  6. #6
    LittleJava is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default Re: Display Patterns use Nested Loops

    Quote Originally Posted by SurfMan View Post
    See the pattern?
    You also don't need the % operator. If your loops are correct, you only need to print i*k.
    thanks again , i see where i need to alter ,
    This is 2nd pattern it should looks like diamonds , i am almost done it but i face some problems were the output looks like this :

    Java Code:
             *
            ***
           *****
            ***
              *
    Java Code:
    class Pattern2 {
      public static void main(String[] args) {
        for (int i = 1; i <=6; i += 2) {
          for (int j = 0; j < 9 - i / 2; j++)
            System.out.print(" ");
    
          for (int j = 0; j < i; j++)
            System.out.print("*");
    
          System.out.print("\n");
        }
    
        for (int i = 3; i > 0; i -= 2) {
          for (int j = 0; j < 5 - i / 2; j++)
            System.out.print("  ");
    
          for (int j = 0; j < i; j++)
            System.out.print("*");
    
          System.out.print("\n");
        }
      }
    }

  7. #7
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: Display Patterns use Nested Loops

    your 4th pattern is easy:
    Java Code:
    public class pattern_4
    {
        public static void p4(int n)
        {
            int i;
            for(i=1;i<=n;i++)
            {
                System.out.println("*********");
            }
        }
    }

  8. #8
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: Display Patterns use Nested Loops

    your 2nd pattern:
    Java Code:
    public class pattern_2
    {
        public static void p2()
        {
            int i,j;
            for(i=1;i<=6;i+=2)
            {
                for(j=1;j<=i;j++)
                {
                    System.out.print("*");
                }
                System.out.println();
            }
            i=1;
            for(i=1;i<=4;i+=2)
            {
                for(j=3;j>=i;j--)
                {
                    System.out.print("*");
    
                }
                System.out.println();
            }
        }
    }

  9. #9
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    929
    Rep Power
    2

    Default Re: Display Patterns use Nested Loops

    Quote Originally Posted by harshit shah View Post
    your 2nd pattern:
    Java Code:
    public class pattern_2
    ...
    }
    Although the OP probably will with no doubt appreciate your hard work, it's still *his* assignment for school that your are doing *for* him. Don't spoonfeed homeworkers here. It's an assignment for a reason.

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default Re: Display Patterns use Nested Loops

    @harshit shah: spoonfeeding is not allowed here (and neither in a lot of other technical forums); because I'm such a nice guy I'll let you get away with it but rumours say that other moderators exist who'd love to shred you in the... shudder... rotating-knives machine before banning your remains ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    LittleJava is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default Re: Display Patterns use Nested Loops

    I am really appreciate , all the help but i am prefer do from scratch on my own. i post it just as a reference where i can make even more. so the 2nd pattern i do with this way.

    Java Code:
        import java.util.Scanner;  
          
        public class Square {  
        public static void main(String[] args)  
            {  
          
                Scanner input = new Scanner(System.in);  
          
                System.out.println("Welcome! This program will take your\n" +  
                                   "number input, and generate a square \n" +  
                                   "of stars.\n");
                                     
                System.out.print("Please enter a number: ");  
                int number = input.nextInt();  
          
                for (int i=1; i<=number; i++)  
                {  
                    for (int j=1; j<=number; j++)
                    	  
                    System.out.print("*");  
                    System.out.println();  
                }  
            }  
        }

  12. #12
    LittleJava is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default Re: Display Patterns use Nested Loops

    Now i am moving on to another code , which is using math formula. i got the basic but i got this error keep bugging me. Really don't know what wrong with my code, hoping someone can tell which part that i am do wrong.

    Java Code:
       import java.util.Scanner;
    
    public class Area
     {
    
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    
    System.out.print("Enter first two points (x1, y1) :");
    double x1 = input.nextDouble();
    double y1 = input.nextDouble();
    
    System.out.print("Enter second two points (x2, y2) :");
    double x2 = input.nextDouble();
    double y2 = input.nextDouble();
    
    double distance = Math.pow(((x2 - x1)(x2 - x1) + (y2 - y1)(y2 - y1)), 0.5);
    
    System.out.print("The distance of the two points is " + distance);
    }
    }
    The Error at the line 17 which is unexpected type. how to solve this problem.

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default Re: Display Patterns use Nested Loops

    @LittleJava: Don't indent your code like that: the second System.out.println() statement doesn't belong to the inner loop while the indentation suggests otherwise.

    kind regards,

    Jos

    ps. w.r.t. your next reply: Java doesn't know about implicit multiplication signs.
    Last edited by JosAH; 02-15-2013 at 06:25 PM.
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: Display Patterns use Nested Loops

    Java doesn't quite understand the mathematical notation on line 17.
    (x2 - x1)(x2 - x1) should be rewritten to ((x2 - x1)*(x2 - x1)). This also applies to the second half of the equation.

    Regards.

  15. #15
    LittleJava is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default Re: Display Patterns use Nested Loops

    Quote Originally Posted by Ronin View Post
    Java doesn't quite understand the mathematical notation on line 17.
    (x2 - x1)(x2 - x1) should be rewritten to ((x2 - x1)*(x2 - x1)). This also applies to the second half of the equation.

    Regards.
    I try to do just what you suggest but when its compile no problems

    Java Code:
    double distance = Math.pow((((x2 - x1)*(x2 - x1)) + ((y2 - y1)*(y2 - y1))), 0.5);
    The Output shown like this :

    Java Code:
    Usage: java [-options] class [args...]
               (to execute a class)
       or  java [-options] -jar jarfile [args...]
               (to execute a jar file)
    where options include:
        -d32	  use a 32-bit data model if available
        -d64	  use a 64-bit data model if available
        -server	  to select the "server" VM
        -hotspot	  is a synonym for the "server" VM  [deprecated]
                      The default VM is server.
    
        -cp <class search path of directories and zip/jar files>
        -classpath <class search path of directories and zip/jar files>
                      A ; separated list of directories, JAR archives,
                      and ZIP archives to search for class files.
        -D<name>=<value>
                      set a system property
        -verbose:[class|gc|jni]
                      enable verbose output
        -version      print product version and exit
        -version:<value>
                      require the specified version to run
        -showversion  print product version and continue
        -jre-restrict-search | -no-jre-restrict-search
                      include/exclude user private JREs in the version search
        -? -help      print this help message
        -X            print help on non-standard options
        -ea[:<packagename>...|:<classname>]
        -enableassertions[:<packagename>...|:<classname>]
                      enable assertions with specified granularity
        -da[:<packagename>...|:<classname>]
        -disableassertions[:<packagename>...|:<classname>]
                      disable assertions with specified granularity
        -esa | -enablesystemassertions
                      enable system assertions
        -dsa | -disablesystemassertions
                      disable system assertions
        -agentlib:<libname>[=<options>]
                      load native agent library <libname>, e.g. -agentlib:hprof
                      see also, -agentlib:jdwp=help and -agentlib:hprof=help
        -agentpath:<pathname>[=<options>]
                      load native agent library by full pathname
        -javaagent:<jarpath>[=<options>]
                      load Java programming language agent, see java.lang.instrument
        -splash:<imagepath>
                      show splash screen with specified image
    See http://www.oracle.com/technetwork/java/javase/documentation/index.html for more details.
    
    Process completed.
    I am using JCreator 5.0. The source Code problem or my compiler ?

  16. #16
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: Display Patterns use Nested Loops

    Quote Originally Posted by JosAH View Post
    @harshit shah: spoonfeeding is not allowed here (and neither in a lot of other technical forums); because I'm such a nice guy I'll let you get away with it but rumours say that other moderators exist who'd love to shred you in the... shudder... rotating-knives machine before banning your remains ...

    kind regards,

    Jos
    let the shredding begin.
    ++++ [NOW I AM LITTLE SHAMELESS. i.e. fuberable decremented me by 2x for loop with -- function.]
    Last edited by harshit shah; 02-16-2013 at 05:57 PM. Reason: negging me.[--]

Similar Threads

  1. Nested loops, and backwards patterns
    By Evildoer in forum New To Java
    Replies: 5
    Last Post: 11-24-2012, 07:38 AM
  2. Using Nested Loops
    By son012189 in forum New To Java
    Replies: 6
    Last Post: 10-02-2011, 05:30 AM
  3. Replies: 20
    Last Post: 03-22-2011, 04:02 AM
  4. Nested Loops
    By ks1615 in forum New To Java
    Replies: 4
    Last Post: 02-18-2009, 02:48 AM
  5. Nested loops?
    By gabriel in forum New To Java
    Replies: 4
    Last Post: 08-06-2007, 04:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •