Register and join over 40,000 other developers!
Recent Topics
-
The Game You Are Waiting For?
WendellHarper - Dec 06 2020 01:21 PM
-
Quora and Reddit Backlinks
WendellHarper - Dec 06 2020 01:14 PM
-
Delete account
pindo - Jul 23 2020 01:33 AM
-
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
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

6 replies to this topic
#1
Posted 10 March 2011 - 06:53 AM
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
*******
******
*****
****
*****
******
*******
output of the program:
How many stars you want to print in 1st row ? 7
*******
******
*****
****
*****
******
*******
#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.
If the user inputs n you will have to do something like this:
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++;
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
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

#7
Posted 11 March 2011 - 05:15 AM
yes that was my fault that i did not specified...
Oh well, you didn't specify the behaviour for even numbers so I just assumed it
Also tagged with one or more of these keywords: printing
![]() Printing a string after If statementStarted by Bitlynk, 15 Apr 2013 ![]() |
|
![]() |
||
Language Forums →
HTML, CSS and Javascript →
Keeping content together when printingStarted by hinkeltje, 21 Feb 2013 ![]() |
|
![]() |
||
Language Forums →
HTML, CSS and Javascript →
Force printing on A4 landscape formatStarted by hinkeltje, 21 Feb 2013 ![]() |
|
![]() |
||
Language Forums →
PHP →
how to shuffle contents fetched from database before printingStarted by alamin, 13 Feb 2013 ![]() |
|
![]() |
||
Language Forums →
C and C++ →
[C++] Printing a whole text file in separate linesStarted by qyler, 01 Feb 2013 ![]() |
|
![]() |
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download