Register and join over 40,000 other developers!
Recent Topics
-
Print specific values from dictionary with a specific key name
Siten0308 - Jun 20 2019 01:43 PM
-
Learn algorithms and programming concepts
johnnylo - Apr 23 2019 07:49 AM
-
Job Gig PHP Form Needed
PJohnson - Apr 18 2019 03:55 AM
-
How to make code run differently depending on the platform it is running on?
xarzu - Apr 05 2019 09:17 AM
-
How do I set a breakpoint in an attached process in visual studio
xarzu - Apr 04 2019 11:47 AM
Recent Blog Entries
Recent Status Updates
Popular Tags
- networking
- Managed C++
- stream
- console
- database
- authentication
- Visual Basic 4 / 5 / 6
- session
- Connection
- asp.net
- import
- syntax
- hardware
- html5
- array
- mysql
- java
- php
- c++
- string
- C#
- html
- loop
- timer
- jquery
- ajax
- javascript
- programming
- android
- css
- assembly
- c
- form
- vb.net
- xml
- linked list
- login
- encryption
- pseudocode
- calculator
- sql
- python
- setup
- help
- game
- combobox
- binary
- hello world
- grid
- innerHTML

16 replies to this topic
#1
Posted 08 March 2011 - 05:19 AM
I need code for the following output. If anybody have please post it
*******
******
*****
****
*******
******
*****
****
#2
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
Anyway, first try. Fail, then we help: http://forum.codecal...html#post282982
#3
Posted 08 March 2011 - 09:28 AM
i tried the following code but cannot put leading space
output of the code is -----
How many stars you want to print in first row ? 5
*****
***
*
but i need
*****
****
***
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
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.
Simply add another loop before the output of the stars for adding spaces.
#5
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.
#6
Posted 09 March 2011 - 05:49 AM
That's the structure you need:
the amount of spaces is 0,1,2,3 so finding the ?? for the 1st inner loop is simple, just k<=i
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
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
#8
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:
Math.ceil() rounds the number up, so 3.5 becomes 4.
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
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?
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.******* ***** *** *
#10
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
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
Posted 22 April 2011 - 07:34 AM
I would use something like this:
Nested loop occurs, when println() prints the char array in String.
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
Posted 02 May 2011 - 09:19 AM
thanks to all
Also tagged with one or more of these keywords: for loop, loop
Language Forums →
PHP →
Content and title won't show on certain page in WordpressStarted by Mello, 05 Jan 2016 ![]() |
|
![]() |
||
![]() Average does not calculate after For LoopStarted by dreddooo, 22 Apr 2015 ![]() |
|
![]() |
||
Language Forums →
C# →
Create Staircase with Nested LoopsStarted by StainedSilva, 12 Mar 2015 ![]() |
|
![]() |
||
Language Forums →
Other Languages →
Bash / Shell Scripting →
Here is a task... Find Nothing :PStarted by ShaunPrawn, 07 Aug 2014 ![]() |
|
![]() |
||
General Forums →
General Programming →
Trying to do a working loopStarted by Error, 23 Jun 2014 ![]() |
|
![]() |
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download