Monday 19 June 2017

Java Code to compute the 1/n!

Problem:

For a given n, design an algorithm to compute 1/n!.

Solution:

package com.myprograms;

import java.util.Scanner;

public class ReciprocalsOfFactorialComputation {

public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("enter how many numbers you want to do factorial");
int n = s.nextInt();
int product = 1;
for(int i = 1; i <=n; i++){
product = product * i;
}
System.out.println("1/n! is: " + 1.0/product);
s.close();
}

}


Output:
enter how many numbers you want to do factorial
5
1/n! is: 0.008333333333333333


No comments:

Post a Comment

Program for primality test in JAVA

Problem: ============= Program for primality test in JAVA What is primality test? A primality test is an algorithm for determining wh...