Write a java program to print factorial of a number. In this tutorial, we are going to write a java code to print factorial of a number.

This question is generally asked in an interviews at fresher level. So first try to solve this problem on it's own.

Difference between iteration and recursion

C program to print factorial of a Number

C++ program to print factorial of a Number

Java programs

Factorial of a input number is : 120

Sorting Algorithm and Their Time Complexities

This question is generally asked in an interviews at fresher level. So first try to solve this problem on it's own.

Difference between iteration and recursion

C program to print factorial of a Number

C++ program to print factorial of a Number

Java programs

## Java Program to Print Factorial of a Number

package factorial; import java.util.*; public class Factorial { /** * @param args the command line arguments */ public static void main(String[] args) { int num, fact = 1; Scanner in = new Scanner(System.in); System.out.println("Enter a number "); /* Taking input from user */ num = in.nextInt(); /* If input number is greater than zero */ if (num > 0) { /** * Suppose if user has input 4 * 4 * 3 * 2 * 1 */ for (int i = 1; i <= num; i++) { fact = fact * i; } System.out.println("Factorial of a number is "+ fact); } else if ( num == 0) { System.out.println ("Factorial of a zero is 1"); } else if (num < 0) { System.out.println("Factorial of a negative number doesn't exist"); } } }

**Output :****Enter a number to calculate it's factorial : 5**

Factorial of a input number is : 120

Sorting Algorithm and Their Time Complexities

## No comments:

## Post a Comment