google.com, pub-8786015629279405, DIRECT, f08c47fec0942fa0 Write a Program to Calculate Factorial of a Number in Java

Write a Program to Calculate Factorial of a Number in Java

1
Write a Program to Calculate Factorial of a Number in Java



//Write a Program to Calculate Factorial of a Number in Java

import java.io.*;
import java.util.*;

class fact
{
public static void main (String args[])
{
int  i,fact=1,n;
n=5;
for(i=1;i<=n;i++)
{
fact=fact*i;
}
System.out.println(“The Factorial of Number ” +n+” is =”+fact);
}
}

Output:

The Factorial of Number 5 is=120

Using Scanner Class


import java.io.*;
import java.util.*;

class fact
{
public static void main (String args[])
{
int  i,fact=1,n;

Scanner sr=new Scanner(System.in);
System.out.println(“Enter the value of factorial\n”)
n=sr.nextInt();
for(i=1;i<=n;i++)
{
fact=fact*i;
}
System.out.println(“The Factorial of Number ” +n+” is =”+fact);
}
}

Output:

The Factorial of Number 5 is=120

Post a Comment

1 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Thank you for your interest 😊

We will back shortly after reviewing...

Thank you for your interest 😊

We will back shortly after reviewing...

Post a Comment

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top