I need code for the following output. If anybody have please post it
*******
******
*****
****
13 replies to this topic
#1
Posted 08 March 2011 - 05:19 AM
|
|
|
#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
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.
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.
wim DC said:
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
#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?
wim DC said:
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.
******* ***** *** *
#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
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top










