Jump to content

Star Pattern using nested for loop

- - - - -

  • Please log in to reply
13 replies to this topic

#1
arunjib

arunjib

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
I need code for the following output. If anybody have please post it
*******
******
*****
****

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
How did this end up at the tutorials?
Anyway, first try. Fail, then we help: http://forum.codecal...html#post282982

#3
arunjib

arunjib

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
i tried the following code but cannot put leading space
import java.io.*;

public class pattern1

{

public static void main (String[] args) throws IOException

{

InputStreamReader read = new InputStreamReader(System.in);

BufferedReader in = new BufferedReader(read);

int i,j,n=0,k;

System.out.print("How many stars you want to print in first row ? ");

n=Integer.parseInt(in.readLine());

for (i = 0; i<n; i=i+2)

{

for (j=i; j<n;j++)

System.out.print ("*");

System.out.println();

}

}

}

output of the code is -----
How many stars you want to print in first row ? 5
*****
***
*
but i need
*****
****
***

#4
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
If you know the top row is 5, and the second amount of stars is 3, you know the difference is 2. Half of two is 1, which is also the number of spaces you want.

Simply add another loop before the output of the stars for adding spaces.

#5
arunjib

arunjib

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
I can think that logic but i cannot place the for loop before output of stars if possible just tell me the loop and tell in what position the loop shout apply i tried in several way but nothing fruitlul

BlaineSch said:

If you know the top row is 5, and the second amount of stars is 3, you know the difference is 2. Half of two is 1, which is also the number of spaces you want.

Simply add another loop before the output of the stars for adding spaces.


#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
That's the structure you need:

int rows = Math.ceil(n/2);

for (i = 0; i<rows; i++)

{

  for(int k=0 ; ??; k++ ){

    System.out.print(" ");

  }

  for (j=0; ?? ;j++)

    System.out.print ("*");   

  }

  System.out.println();

}


the amount of spaces is 0,1,2,3 so finding the ?? for the 1st inner loop is simple, just k<=i

Edited by wim DC, 09 March 2011 - 06:27 AM.


#7
arunjib

arunjib

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
i dont know the use of Math.ceil() i want to read about it first. then i shall try this code.

wim DC said:

That's the structure you need:

int rows = Math.ceil(n/2);

for (i = 0; i<rows; i++)

{

  for(int k=0 ; ??; k++ ){

    System.out.print(" ");

  }

  for (j=0; ?? ;j++)

    System.out.print ("*");   

  }

  System.out.println();

}


the amount of spaces is 0,1,2,3 so finding the ?? for the 1st inner loop is simple, just k<=i


#8
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Well, to calculate the amount of rows, it's basicly n/2
like 6 are 3 rows required:
******

 ****

  **
But 7 are 4 rows, since 7/2 = 3.5, and integers don't accept commas, this will become 3 for Java.
Math.ceil() rounds the number up, so 3.5 becomes 4.

*******

 *****

  *** 

   *



#9
arunjib

arunjib

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
for Math.ceil() which library class should be imported?
should i put any value in substitute of ?? for both or the for loop?

wim DC said:

Well, to calculate the amount of rows, it's basicly n/2
like 6 are 3 rows required:
******

 ****

  **
But 7 are 4 rows, since 7/2 = 3.5, and integers don't accept commas, this will become 3 for Java.
Math.ceil() rounds the number up, so 3.5 becomes 4.

*******

 *****

  *** 

   *



#10
tharindhu

tharindhu

    Newbie

  • Members
  • Pip
  • 1 posts
#include<stdio.h>
int main()
{
int x;
int y;
int z;
for(x=0;x<7;x=x++)
{
printf("\n");
for(z=-x;z<0;z++)
{
printf(" ");
}
for(y=x*2;y<7;y++)
{
printf("*");

}
}

}


try this

#11
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
I would use something like this:

public static void draw(int amount){
        char[] pat = new char[amount];
        Arrays.fill(pat, '*');
        for(int i = 0; i < amount/2+1; i++){
            System.out.println(new String(pat));    
            pat[i] = pat[pat.length-i-1] = ' ';        
        }        
}
***************
 ************* 
  ***********  
   *********   
    *******    
     *****     
      ***      
       *    

Nested loop occurs, when println() prints the char array in String.

#12
arunjib

arunjib

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
thanks to all




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users