i want to get following out for a java program... how i should start my study?
output of the program:
How many stars you want to print in 1st row ? 7
*******
******
*****
****
*****
******
*******
6 replies to this topic
#1
Posted 10 March 2011 - 06:53 AM
|
|
|
#2
Posted 10 March 2011 - 07:35 AM
If the user inputs n you will have to do something like this:
Now, think about that "print a line" statement.
If you need some more help just ask =)
print '*' n times; for(int i=0;i<n-2;i++) print a line; print '*' n more times;
Now, think about that "print a line" statement.
If you need some more help just ask =)
#3
Posted 11 March 2011 - 03:50 AM
i applied the code as follows---
for(i=0;i<n;i++)
System.out.print("*");
for(i=0;i<n-2;i++)
System.out.println();
System.out.print("*");
but output is not come. i need some spaces to print please give me more clue.eafkuor said:
If the user inputs n you will have to do something like this:
Now, think about that "print a line" statement.
If you need some more help just ask =)
print '*' n times; for(int i=0;i<n-2;i++) print a line; print '*' n more times;
Now, think about that "print a line" statement.
If you need some more help just ask =)
#4
Posted 11 March 2011 - 04:17 AM
String y=JOptionPane.showInputDialog("Number: ");
int n=Integer.parseInt(y);
for(int i=0;i<n;i++)System.out.print("*");
System.out.println();
int j;
for(j=1;j<n/2;j++){
int spaces=j;int middle=n-2*spaces-2; //you put the first '*' after j spaces, the second one after another n-2spaces-2
for(int k=0;k<spaces;k++)System.out.print(" ");
System.out.print("*");
for(int k=0;k<middle;k++)System.out.print(" ");
System.out.println("*");
}
if(n%2!=0){//you print the middle '*' only if n is odd
for(int k=0;k<j;k++)System.out.print(" ");
System.out.println("*");
}
for(j--;j>0;j--){
int spaces=j;int middle=n-2*spaces-2;
for(int k=0;k<spaces;k++)System.out.print(" ");
System.out.print("*");
for(int k=0;k<middle;k++)System.out.print(" ");
System.out.println("*");
}
for(int i=0;i<n;i++)System.out.print("*");
}
#5
Posted 11 March 2011 - 04:52 AM
Thank you very much for the code. There is only one logical error [if we input even number the shape will not come] in the above code. I just applied the following code
if(n%2==0) n++;
eafkuor said:
String y=JOptionPane.showInputDialog("Number: ");
int n=Integer.parseInt(y);
for(int i=0;i<n;i++)System.out.print("*");
System.out.println();
int j;
for(j=1;j<n/2;j++){
int spaces=j;int middle=n-2*spaces-2; //you put the first '*' after j spaces, the second one after another n-2spaces-2
for(int k=0;k<spaces;k++)System.out.print(" ");
System.out.print("*");
for(int k=0;k<middle;k++)System.out.print(" ");
System.out.println("*");
}
if(n%2!=0){//you print the middle '*' only if n is odd
for(int k=0;k<j;k++)System.out.print(" ");
System.out.println("*");
}
for(j--;j>0;j--){
int spaces=j;int middle=n-2*spaces-2;
for(int k=0;k<spaces;k++)System.out.print(" ");
System.out.print("*");
for(int k=0;k<middle;k++)System.out.print(" ");
System.out.println("*");
}
for(int i=0;i<n;i++)System.out.print("*");
}
#6
Posted 11 March 2011 - 05:13 AM
arunjib said:
Thank you very much for the code. There is only one logical error [if we input even number the shape will not come] in the above code. I just applied the following code
if(n%2==0) n++;
Oh well, you didn't specify the behaviour for even numbers so I just assumed it :D
#7
Posted 11 March 2011 - 05:15 AM
yes that was my fault that i did not specified...
eafkuor said:
Oh well, you didn't specify the behaviour for even numbers so I just assumed it :D
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









