• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Nested Loops

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got this code from Beginning Java 2 JDK 1.2.
public class Factorial {
public static void main(String[] args){
long limit = 20;
long factorial = 1;
for(int i=1; i <= limit; i++){
factorial = 1;
for(int factor = 2; factor <= i; factor++){
factorial *= factor++;
System.out.println(i + " ! " + " is " + factorial );
}
}
}
I get the first part of the "For" loop but when it hits the second "For" loop. I see the loop_condition "factor <= i" never being true. Is the first time through the loop factor = 2 and i = 1. If so, isn't it false. I have changed the relational operation, <=, to different ones to see what would happen. Turns out, only <= works. Help!

------------------
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stephanie Drafts:
I got this code from Beginning Java 2 JDK 1.2.
public class Factorial {
public static void main(String[] args){
long limit = 20;
long factorial = 1;
for(int i=1; i <= limit; i++){
factorial = 1;
for(int factor = 2; factor <= i; factor++){
factorial *= factor++;
System.out.println(i + " ! " + " is " + factorial );
}
}
}
I get the first part of the "For" loop but when it hits the second "For" loop. I see the loop_condition "factor <= i" never being true. Is the first time through the loop factor = 2 and i = 1. If so, isn't it false. I have changed the relational operation, <=, to different ones to see what would happen. Turns out, only <= works. Help!


This is because the first time thru factor is neither greater than i or less to it so it is false and falls out.You can say something like >= so that factor will always be greater than i.
------------------
Sun Certified Programmer on the Java 2 Platform
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To best solve your problem, it should be completely understood first.
The mathematical way of finding the factorial is:
N ! = Summ (K = 0 to N) of : (N K) * ((-1) ^ K) * (( N - K) ^ N) where (N K) is the binomial coefficient;
Example: 4! = 4*3*2*1 and 5! = 5*4*3*2*1
Now that is known your program can be altered to work out the factorial correctly, like this:

 
I hired a bunch of ninjas. The fridge is empty, but I can't find them to tell them the mission.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic