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
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();
}
}
enter how many numbers you want to do factorial
5
1/n! is: 0.008333333333333333
No comments:
Post a Comment