Menu
Topics Index
...
`

Print Factorial


If you need explanation Read this topic

Print Factorial
What will be the output of the following program?
public class FactIs {
    public static void main(String[] args) {
        int number = 6;
        int fact = 1;
        while (number > 1) {
            fact *= number;
            number--;
        }
        System.out.println("Factorial for 6 is " + fact);
    }
}

A. Factorial for 6 is 1
B. Factorial for 6 is 120
C. Factorial for 6 is 720
D. Compilation Error
E. Runtime Error
Topic: Factorial Program In Java Using While Loop

If you need explanation Read this topic

User comments below. All of them might not be correct.

Answer is c....given logic is to calculate the factorial of number...initially fact = 1 ...then we multiply fact with 6 then fact = 1×6 =6 until number =1,we are decrementing number by 1 and multiplying the value of fact with number..when number equal to 1 loop breaks and result is printed which is 720

Posted by Shashanka Mogaliraju    2014-11-24 03:50:53


C is the answer factorial for 6 is 720

Posted by Asad Ahmed    2014-11-24 03:52:19


ans is c fact=fact*number
number=6;iteration 1:fact=6
number=5;iteration 2:fact=6*5=30
number=4;iteration 3:fact=30*4=120
nymber=3;iteration 4:fact=120*3=360
number=2;iteration 5:fact=360*2=720
number=1; condition terminates and while loop fails and prints the fact i.e 720

Posted by Sai Veerendra    2014-11-24 04:54:57


B

Posted by Puspita Sahu    2014-11-24 07:35:16


This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Sai Veerendra, Sai Ram,  for 'Best Comment' and Shashanka Mogaliraju for the 'Popular Comment'. The 'lucky liker' is Bolo Raven. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-11-25 02:30:57


© meritcampus 2019

All Rights Reserved.

Open In App