I just want to say hello to the Ranch and I like the site it has helped me up till now. This is where I am stuck now. Thank you
Ryan Perlman
I have written the code for a factorial program now I am having trouble nesting it into a program to compute the constant e,
the factorial is 5! = 5*4*3*2....
the e is e= 1 +1/1!+1/2!+1/3!....
public class NewFact
{
public static void main (
String [] args)
{
String input;
int n;
int fact = 1;
input = JOptionPane.showInputDialog("enter a number to compute");
n = Integer.parseInt(input);
while( n > 1 ){
fact *= n;
n = n-1;
System.out.println(fact);
}
}
}
My second question is:
I have written a for loop to print out a right triangle like so
up to ten '*' but now I want it to print 4 of them side by side
everytime I nest in another for loop I either distort my first triangle or create an infinite loop.
*
**
***
****
*****
My first for loop is
public class Triangle
{
public static void main(String [] args)
{
for( int i = 1; i <= 10; i++){
for ( int j = 1; j <= i; j++){
system.out.print('*');
}
System.out.print();
}
}
}
thanks for any pointers in the right direction
Ryan Perlman