# [SOLVED] for loop help!

• 02-07-2009, 05:36 AM
PureAwesomeness
[SOLVED] for loop help!
i wonder why that the highlighted part doesnt work for number 24?
it works fine when i enter 6.

Code:

```import java.util.Scanner;         public class sample{                 public static void main (String[]args){         Scanner input = new Scanner(System.in);         System.out.println("Enter a positive integer number");                 int num1 = input.nextInt();         System.out.println("Enter a positive integer number");                 int num2 = input.nextInt();         int max = 1;         if (num1 >= num2){                 max = num1; }         else if (num1 <= num2){                 max = num2;         }         System.out.println("The max is: "+ max); //factorial         int total = 1;         if (max <=8){         for(int fac = max; fac >= 1; fac--){                 total = total*fac; }         System.out.println(max+"!"+ "is " + total); } [COLOR="Teal"]//check if max is factorial of any number         int total1 = 1; for (int tri = 1; tri<max; tri++){         for (int fac1 = tri; fac1 >= 1; fac1--){                 total1 = total1 * fac1;         if (total1 == max){         System.out.println(max+" is the factorial of "+ tri); } } }[/COLOR]         int sum = 0; //perfect number         for (int per = 1; per < max; per++){                 if( max % per == 0){                 sum = sum + per;         }         }                 if (sum == max){                 System.out.println( max + " is a perfect number");         }                 else                 System.out.println(max + " is not a perfect number");         }                 }```
• 02-07-2009, 06:15 AM
mtyoung
you need to initialize total1 to 1 at right position

and your algo is not correct enough...
you know .. 2! = 2, 1! = 1
• 02-07-2009, 06:44 AM
PureAwesomeness
Quote:

Originally Posted by mtyoung
you need to initialize total1 to 1 at right position

and your algo is not correct enough...
you know .. 2! = 2, 1! = 1

ok i changed it and it works.
but i don't get why can't i initialize total1 at that position?
• 02-07-2009, 07:01 AM
mtyoung
after you try to test max is 1!, then try to test for 2!, so on...
after trying 2!, total1 is 2, and you try to test for 3!,
total1 start from 2, not 1