nht_cntt

Giai thừa - java

Oct 11th, 2018
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.71 KB | None | 0 0
  1. package baitap;
  2. import java.util.Scanner;
  3. public class baitap {
  4. public static int n;
  5.     //---------ham tinh giai thua--------------
  6.     public static double giaithua(int n){
  7.         double tmp = 1;
  8.         if (n>1) {
  9.             for (int i = 2; i <= n; i++) {
  10.                 tmp *= i;
  11.             }
  12.         }
  13.         return tmp;
  14.     }
  15.     /**
  16.     * @param args the command line arguments
  17.     * S = 1/1! + 1/2! + 1/3! + ... + 1/n!
  18.     */
  19.     public static void main(String[] args) {
  20.         // TODO code application logic here
  21.         float tong = 0.0f;
  22.         Scanner reader = new Scanner(System.in);
  23.         System.out.print("Nhap so tu nhien N = ");
  24.         n = reader.nextInt();
  25.         for (int i = 1; i <= n; i++) {
  26.             tong += 1/giaithua(i);
  27.         }
  28.         System.out.println("tong day so la: " + tong);
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment