top of page

 

 

 

Java Iterations - Hands on 2

 

Factorials of input numbers

Write a program to read 5 numbers and print factorials of each.

 

(Final answers should be non decimal numbers).

 

 

 

Example:

 

Input:

 

2

3

4

6

5

 

Output:

 

2

6

24

720

120

​

 Solutions:


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
 public static void main(String args[] ) throws Exception {
 /* Enter your code here. Read input from STDIN. Print output to STDOUT */
 Scanner scn=new Scanner(System.in);
 int []num=new int[5];
 for(int i=0;i<5;i++)
 {
 num[i]=scn.nextInt();
 String res=factorial(num[i]);
 System.out.println(res);
 }
 }
 public static String factorial(int n)
 {
 BigInteger fact=new BigInteger("1");
 for(int i=1;i<=n;i++){
 fact=fact.multiply(new BigInteger(i+""));
 }
 return fact.toString();
 }
}

Java Iterations - Hands on 2: About

©2018 by The real one. Proudly created with Wix.com

bottom of page