954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Java Printing Patterns using for-loops

Hello! I have to print patterns using '*' and I am at a loss how to get these patterns printed!

I figured out the first one,
*
**
***
****
*****
******
*******
********
*********
**********

and now i have to do it upside down:
**********
*********
********
*******
******
*****
****
***
**
*

and backwards:
**********
_*********
__********
___*******
____******
_____*****
______****
_______***
________**
_________*
and

_________*
________**
_______***
______****
_____*****
____******
___*******
__********
_*********
**********

this is the code i have for the first one:

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


and I figured if I just changed the numbers around and made i go from 10 to zero it would print out but it is not working. any help??? please!!

Thanks!

letters1417
Newbie Poster
9 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Hello! I have to print patterns using '*' and I am at a lose how to get these patterns printed!

I figured out the first one, * ** *** **** ***** ****** ******* ******** ********* **********

and now i have to do it upside down: ********** ********* ******** ******* ****** ***** **** *** ** *

and backwards: ********** ********* ******** ******* ****** ***** **** *** ** * and

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

this is the code i have for the first one:

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

and I figured if I just changed the numbers around and made i go from 10 to zero it would print out but it is not working. any help??? please!!

Thanks!


wouldnt you have to swap the loops around too? or maybe the sign from +->- or both. Havent tested just thinking logically now

DavidKroukamp
Practically a Master Poster
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
 

actually, yes. but you also have to change the +'s to -'s, and < to >.
can you show the changed code you tried?

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 
actually, yes. but you also have to change the +'s to -'s, and < to >. can you show the changed code you tried?
for(int k = 10; k > 0; k--){
            for(int l=10; l > k; l--){
                System.out.print('*');
            }
            System.out.println();
        }


however this gives me exactly what I get in part a.

letters1417
Newbie Poster
9 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 
for(int k = 10; k > 0; k--){
            for(int l=10; l > k; l--){
                System.out.print('*');
            }
            System.out.println();
        }

however this gives me exactly what I get in part a.


where is the -1 you had going in the original algorithm ?

DavidKroukamp
Practically a Master Poster
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
 

you don't have to completely turn around the inner for loop.
and, in that for loop, you don't need to compare it to '10' but to the value of 'k'

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 
where is the -1 you had going in the original algorithm ?

I have it there now and still does the same as the first one!

letters1417
Newbie Poster
9 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 
you don't have to completely turn around the inner for loop. and, in that for loop, you don't need to compare it to '10' but to the value of 'k'

What do you mean? Could you put it in code for me?
[EDIT!]

I got it! Now am just working on the backwards ones!

letters1417
Newbie Poster
9 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

the backward ones are just those two you already have printed right after each other.
you already have the code, just make sure its re-usable

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 
the backward ones are just those two you already have printed right after each other. you already have the code, just make sure its re-usable

I got part c but it is not aligned correctly....

letters1417
Newbie Poster
9 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 
I got part c but it is not aligned correctly....


how is it not aligned? can you show the algorithm?

DavidKroukamp
Practically a Master Poster
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
 

part c? what is part c?
in this backwards you have to decide what to print
at first: the number of spaces, then the number of *'s
and this for each line

find a pattern to print this shape, and write the code for it

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

For your second attempt... Try a regular ascending loop from 1 to K.

Sorry... I posted before reading stultuske's post.

hfx642
Posting Pro
515 posts since Nov 2009
Reputation Points: 248
Solved Threads: 105
 
how is it not aligned? can you show the algorithm?


I thought of asking that too, but in this thread, his backwards is different from in the other thread :)

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 
I thought of asking that too, but in this thread, his backwards is different from in the other thread :)

the different parts a, b, c and d are the four different patterns that needed to be printed. in the end I figured out this code:

public static void problem7(){
        System.out.println("Part a)");
        for (int i =0; i < 10; i++)
        {
            for (int j=0; j < i+1; j++)
            {
                System.out.print('*');
            }
            System.out.println();
        }
        System.out.println("Part b)");
        for(int k = 10; k > 0; k--){
            for(int l=0; l < k-1; l++){
                System.out.print('*');
            }
            System.out.println();
        }
        System.out.println("Part c)");
        for(int k = 10; k > 0; k--){
            for(int l=0; l < k-1; l++){
                System.out.print(' ');
            }
            for(int n=10; n > k-1; n--){
                System.out.print('*');
            }
            System.out.println();
        }
        System.out.println("Part d)");
        for(int k = 0; k < 10; k++){
            for(int l=1; l < k+1; l++){
                System.out.print(' ');
            }
            for(int n=10; n > k; n--){
                System.out.print('*');
            }
            System.out.println();
        } 
    }


and as for the diamond thread which was a whole different problem, I got this code:

public static void problem8(){
        Scanner input = new Scanner(System.in);  
        System.out.println("Input odd number ");
        String number = input.nextLine();
        int n = Integer.parseInt(number);
        for (int i = 1; i < n+1; i +=2) {
            for (int j = 0; j < n-i / 2; j++)
                System.out.print(" ");
            for (int j = 0; j < i; j++)
                System.out.print("*");
            System.out.print("\n");
        }
        for (int i = n-2; i > 0; i -= 2) {
            for (int j = 0; j < n - i/2; j++)
                System.out.print(" ");
            for (int j = 0; j < i; j++)
                System.out.print("*");
            System.out.print("\n");
        }
    }


I'm sorry if I was a little confusing! I had no background in this type of outputting before receiving these problems. But thank you all for your help, I figured it out :)

letters1417
Newbie Poster
9 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: