Search This Blog

If you have any problem or question please write in the comments. We will try to solve and send you the solution. Not just that we will also post your problems solution on our blog.

Tuesday, 5 July 2011

Write a program to print factorial of any number


import java.util.*;
class factorial
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);

int a,b=1;
System.out.print ("\n\t Enter the number = ");
a=in.nextInt();

while (a>0)
{
b*=a;
--a;
}

System.out.println ("\n\t Factorial = "+b);

}
}

Note:
     First of all computer will check the condition of while loop. If it is true then it will
 multiply b by a. After that it will decrease the value of a by 1 and repeat the same task. This
 will continue until a=0. When a=0 the loop will be broken and the next statement to it will
be executed.

1 comment:

  1. i have a question how we will take different numbers and then it will print the factorial of all the numbers that are entered from the user

    ReplyDelete