Thursday 12 January 2017

Java Program to Calculate Factorial Using Nested Loops

Code:

public class FactWithNestedLoop 
{
public static void main(String args[])
{
for(int i=1;i<=10;i++)
{
long fact=1;
{
System.out.print(i+"=");
for(int j=1;j<=i;j++)
{
fact=fact*j;
}
System.out.println(fact);
}
}
}
}

Output:

1=1
2=2
3=6
4=24
5=120
6=720
7=5040
8=40320
9=362880
10=3628800

No comments:

Post a Comment