0

I have a task where I need to calculate 1 + 1/(1!) + 1/(2!) + 1/(3!) + … + 1/(N!) using one loop statement. I have this code:


public class Main {
    public static void main(String[] String) {
        Scanner sc = new Scanner(System.in);
        float num = sc.nextInt();
        long factorial = 1;
        for(int i = 1; i <= num; ++i)
        {
            factorial *= i;
        }
        long result = 1/factorial;
        System.out.println(result);
    }
}

But it just returns 0. How do I make it work correctly, so that it would display the actual result? Also, I'm not allowed to use math library...

teheheugh
  • 17
  • 4

0 Answers0