Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Star Pattern using nested for loop

for loop loop

  • Please log in to reply
16 replies to this topic

#1 arunjib

arunjib

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 77 posts
  • Programming Language:C, Java, C++, PHP, (Visual) Basic, JavaScript, Visual Basic .NET, Logo, VBScript
  • Learning:C, Java, C++, PHP, (Visual) Basic, JavaScript, Visual Basic .NET, Logo, VBScript

Posted 08 March 2011 - 05:19 AM

I need code for the following output. If anybody have please post it
*******
******
*****
****
  • 0

#2 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 08 March 2011 - 06:16 AM

How did this end up at the tutorials?
Anyway, first try. Fail, then we help: http://forum.codecal...html#post282982
  • 0

#3 arunjib

arunjib

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 77 posts
  • Programming Language:C, Java, C++, PHP, (Visual) Basic, JavaScript, Visual Basic .NET, Logo, VBScript
  • Learning:C, Java, C++, PHP, (Visual) Basic, JavaScript, Visual Basic .NET, Logo, VBScript

Posted 08 March 2011 - 09:28 AM

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
*****
****
***
  • 0

#4 BlaineSch

BlaineSch

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1559 posts

Posted 08 March 2011 - 12:21 PM

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.
  • 0

#5 arunjib

arunjib

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 77 posts
  • Programming Language:C, Java, C++, PHP, (Visual) Basic, JavaScript, Visual Basic .NET, Logo, VBScript
  • Learning:C, Java, C++, PHP, (Visual) Basic, JavaScript, Visual Basic .NET, Logo, VBScript

Posted 09 March 2011 - 04:36 AM

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

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.


  • 0

#6 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 09 March 2011 - 05:49 AM

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.

  • 0

#7 arunjib

arunjib

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 77 posts
  • Programming Language:C, Java, C++, PHP, (Visual) Basic, JavaScript, Visual Basic .NET, Logo, VBScript
  • Learning:C, Java, C++, PHP, (Visual) Basic, JavaScript, Visual Basic .NET, Logo, VBScript

Posted 09 March 2011 - 09:37 AM

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

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


  • 0

#8 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 09 March 2011 - 09:39 AM

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.
*******
 *****
  *** 
   *

  • 0

#9 arunjib

arunjib

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 77 posts
  • Programming Language:C, Java, C++, PHP, (Visual) Basic, JavaScript, Visual Basic .NET, Logo, VBScript
  • Learning:C, Java, C++, PHP, (Visual) Basic, JavaScript, Visual Basic .NET, Logo, VBScript

Posted 10 March 2011 - 05:40 AM

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

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.
*******
 *****
  *** 
   *


  • 0

#10 tharindhu

tharindhu

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 22 April 2011 - 07:18 AM

#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
  • 0

#11 Sinipull

Sinipull

    CC Addict

  • Validating
  • PipPipPipPipPip
  • 384 posts

Posted 22 April 2011 - 07:34 AM

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.
  • 0

#12 arunjib

arunjib

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 77 posts
  • Programming Language:C, Java, C++, PHP, (Visual) Basic, JavaScript, Visual Basic .NET, Logo, VBScript
  • Learning:C, Java, C++, PHP, (Visual) Basic, JavaScript, Visual Basic .NET, Logo, VBScript

Posted 02 May 2011 - 09:19 AM

thanks to all
  • 0





Also tagged with one or more of these keywords: for loop, loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download