Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a program that, for a given two integer numbers n and x,
- * calculates the sum S = 1 + 1!/x + 2!/x^2 + … + n!/x^n. Use only one loop.
- * Print the result with 5 digits after the decimal point. */
- import java.util.Locale;
- import java.util.Scanner;
- public class _05_CalculateTheSumOfFractions {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Locale.setDefault(Locale.ROOT);
- Scanner scanner = new Scanner(System.in);
- System.out.print("Enter a whole positive number for N: ");
- int numN = scanner.nextInt();
- System.out.print("Enter other whole number for X: ");
- int numX = scanner.nextInt();
- scanner.close();
- if (numN > 0) {
- double sum = 1;
- double nominator = 1;
- double denominator = 1;
- for (int i = 1; i <= numN; i++) {
- nominator *= i;
- denominator *= numX;
- sum += nominator / denominator;
- }
- System.out.printf("The Sum of these Fractions is: %.5f!\n", sum);
- } else {
- System.out.println("Error! - Invalid Input number N!!!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement