Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Make triangle with * & print number (School :()

Hello all...
I am new to these forums and i need some help regarding java for the university... Here are 2 things i have to make as java classes but i dont know how:

1st) Print triagle : Read a positive number posted by user N (N>=3) and print a triangle with asterisks that will be N lines (see example)
(eg N=4)

*
***
*****
*******

Its something with loop but i dunno what :(

2nd) Print number: Read a user submited number from 0 to 99999 and post the ammount of the digits of the number submited. Next print same number (the one submited) in a 5 fields format Eg number 13, digits=2 and print as 00013

If you help me in both i take a good grade for current lab and i will be very happy for it :)

Hope i get replies :WAVE:
[809 byte] By [Seldimi] at [2007-11-11 8:29:31]
# 1 Re: Make triangle with * & print number (School :()
Hint for the first, run the loop from 1 to N and print 2N -1 asterisks on each line.
aniseed at 2007-11-11 22:35:14 >
# 2 Re: Make triangle with * & print number (School :()
hhMMM im new to java (i only used php in the past) so can u be more explainable?
I just dont know what to print and how it print (println or print) in the loop :)
Seldimi at 2007-11-11 22:36:14 >
# 3 Re: Make triangle with * & print number (School :()
I found the solution for 2nd code, its the following :
System.out.print("Input number R:");
int R = Integer.parseInt(br.readLine());
while (R<0 || R>99999) {
System.out.println("Error. Wrong Input. Try Again");
System.out.print("Input number R:");
R = Integer.parseInt(br.readLine());
System.out.print("The digit number is :");
}
int countzero = 0; // Apothikeusi Midenikwn Stixeiwn
if (R<10) {
System.out.println("1 Digit");
countzero = 4;
} else if (R<100) {
System.out.println("2 Digits");
countzero = 3;
} else if (R<1000) {
System.out.println("3 Digits");
countzero = 2;
} else if (R<10000) {
System.out.println("4 Digits");
countzero = 1;
} else if (R<100000) {
System.out.println("5 Digits");
countzero = 0;
}
for (int g=0; g<countzero; g++)
System.out.print("0");
System.out.println(R);
I must make the tree only now
Seldimi at 2007-11-11 22:37:23 >
# 4 Re: Make triangle with * & print number (School :()
for(int i = 1; i <= n; i++) { // n is the number of lines

int numOfSpaces = /* ? */;
int numOfAsterisks = /* ? */;

for(int j = 1; j <= numOfSpaces; j++)
System.out.print(' ');

for(int j = 1; j <= numOfAsterisks; j++)
System.out.print('*');

System.out.print('\n');
}
Try to finish it now. If you don't see it immediatelly, write down on paper the number of spaces and asterixes for each line. Try to see how those number relate to i.
prometheuzz at 2007-11-11 22:38:23 >
# 5 Re: Make triangle with * & print number (School :()
Ok, here's how I did the code for the triangle

import java.util.*;
import javax.swing.JOptionPane;

public class pyramid{
public static void main (String[]Args){

int input = 0;
String temp_string = JOptionPane.showInputDialog("Please enter an integer");
temp_string = temp_string.trim();
input= Integer.parseInt(temp_string);

for (int i=1; i<=input; i++)
{
for (int j=1; j<=i; j++)
{
System.out.print("*");
}
System.out.println("");
}
}
}

This just gets you an output like

*
**
***

as opoosed to being lined up like you said
ftr at 2007-11-11 22:39:20 >
# 6 Re: Make triangle with * & print number (School :()
Thank you a lot, i really appreciated ur help :)
Seldimi at 2007-11-11 22:40:19 >
# 7 Re: Make triangle with * & print number (School :()
import javax.swing.JOptionPane;

public class TriangleWithAsterisk {

public static void main(String[] args)
{
int n;

n = Integer.valueOf(JOptionPane.showInputDialog(null, "Enter the number of lines --> "));

for (int i = 0; i < n; i++)
{
int stars = 1 + 2 * (i);
int space = n - i;
for (int j = 0; j < space; j++)
{
System.out.print(" ");
}

for (int k = 0; k < stars; k++)
{
System.out.print("* ");
}
System.out.println();
}

}
}

i got an output like this when i entered 6

*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * * * *
W3N at 2007-11-11 22:41:23 >
# 8 Re: Make triangle with * & print number (School :()
Geeeeeeeeee thanx a lot :D
Really aprreciated your help!
Seldimi at 2007-11-11 22:42:20 >