-2

Why is this statement used: fact*=i;. I can't understand because I am a beginner. Is there some other way that we can write the same statement in a while loop?

//This is my program for factorial using input from user.

import java.io.*;

public class factorialInput {

    public static void main(String[] args) throws IOException {
        int i = 1;
        int fact = 1;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter any number:");
        int n = Integer.parseInt(br.readLine());
        while (i <= n) {
            fact *= i;
            i++;
        }
        System.out.println("Factorial of" + n + "!" + "=" + fact);
    }
}
candied_orange
  • 7,036
  • 2
  • 28
  • 62
Brian Dsouza
  • 23
  • 1
  • 1
  • `fact = fact * i`, it's short hand, programmers are lazy ;) – MadProgrammer Feb 17 '15 at 06:28
  • 2
    This question is in no way a duplicate of "Using loops to compute factorial numbers, Java". `*=` is a "Compound Assignment Operator". Search using that name to find lots of explanations. – candied_orange Feb 17 '15 at 06:41
  • @CandiedOrange +1 I agree with you. Maybe quite interesting for meta the fact that there are so many questions beeing closed with a wrong marking for duplicate... – oopbase Feb 17 '15 at 07:58
  • I flagged it hoping I wouldn't have to take it that far. – candied_orange Feb 17 '15 at 07:59
  • http://stackoverflow.com/questions/8710619/java-operator is not exactly a duplicate of this question either but it gives some deeper understanding. – candied_orange Feb 17 '15 at 08:12

1 Answers1

2

fact*=i; is equivalent to fact = fact * i;

From tutorialpoints

*= is multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand

codingenious
  • 8,385
  • 12
  • 60
  • 90