JavaTech
Pages - Menu
(Move to ...)
Home
Compilation Process
Algorithms || Introduction
▼
Pages
(Move to ...)
Algorithms || Introduction
Compilation Process
▼
Saturday, December 19, 2015
Java program to print the factorial of a number
Factorial of a number:
import java.util.Scanner;
class Factorial
{
public static void main()
{
int n, c, fact = 1;
System.out.println("Enter a number");
Scanner in = new Scanner(System.in);
n = in.nextInt();
if ( n < 0 )
System.out.println("Number should be non-negative.");
else
{
for ( c = 1 ; c <= n ; c++ )
fact = fact*c;
System.out.println("Factorial of "+n+" is = "+fact);
}
}
}
Thank you and make sure to like and comment.
No comments:
Post a Comment
‹
›
Home
View web version
No comments:
Post a Comment