• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Can somoene help me really quickly with a basic java program

50

Platinum Member
Hey, I am writng a progrma in java to output the pascal triangle.
import TerminalIO.KeyboardReader;

public class TrivialApplication {

public static void main(String args[]) {

KeyboardReader reader = new KeyboardReader();

int integer= reader.readInt("Number of rows: "); //inputs number of rows
int row; //row
int col; //column

for(row=0;row<integer;row++){ //sets the dimensions for rows
for(col=0;col<=row;col++){ //sets the dimensions for columns
System.out.print((factorial(row))/(factorial(col)*factorial(row-col))); //provides formula for creating each row
System.out.print(" "); //adds space for neatness
}

System.out.println(""); //returns a space


}
}
public static int factorial(int n) { //illustrates factorial command
int fact=1;
for(int i=1;i<=n;i++)
fact=fact*i;
return fact;
}


}


I have this so far but it outputs a triangle like
1
1 1
1 2 1

and i need an isoceles triangle. Any help?
 
need to output spaces on the left with the length of the line about to output? so, depending on how large the last line is, output spaces in the previous rows?

 
Thank you for stating the obvious but I really need help right now and software forum doesn't have many people in it
 
you know how many lines total you want to output right? let's say it's n. now you will know how long the last line would be, in terms of n. then you can deduce how many space you need in front of the first line, in terms of n. as well as how many spaces you need in front of the second line, in terms of n, and so on.
 
Originally posted by: 50
How would I know how large the line before it was?

well, isn't this based off of the input of the number of rows? you are going to have to figure out how long you last line is if you want to output it correctly...
 
um...let's see...
if you keep a count of the rows and can figure out the number of total rows ahead of time. then you can add n-1 spaces to the start of each row and that might format it right.

ie

1 - 3 spaces before 1
1 1 - 2 spaces before 1
1 2 1 - 1 spaces before 1
1 2 3 3 - 0 spaces before 1


-Ryan

*edit*

damn spaces not working...
 
and to be really picky. you have to pad your numbers to use up exactly how many spaces no matter how many digits it has
 
Did you do any thinking on your own before posting here, in the wrong forum yet?

The point of a programming class is to learn to solve your own problems, not to copy and paste answers.

If you don't learn while the problems are easy, you're just going to fall farther behind until you fail the final. If you somehow pass the class and take any other programming classes you'll be wofeully unprepared.
 
Originally posted by: stan394
actually, there is a simpler solution involving using an array. think about it

yup...

You can even use (double the rows) to do it. But it is constrained by the left of the horizontal font on your screen.
 
give the guy a break. granted programming is about problem solving he at least tried and apparently got suck. he's not asking people to write the code for him but to advise him what to do next. and stop being asshats about this being the wrong forum, the forum gets a lot of traffic and you complainers are losers with nothing better to do anyways.
 
Back
Top