Computer Applications
Write a program to compute and display the sum of the following series:
S = (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + -------- + (1 + 2 + 3 + ----- + n ) / (1 * 2 * 3 * ----- * n)
Answer
import java.util.Scanner;
public class KboatSeries
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter n: ");
int n = in.nextInt();
double sum = 0.0;
for (int i = 2; i <= n; i++) {
double num = 0.0, den = 1.0;
for (int j = 1; j <= i; j++) {
num += j;
den *= j;
}
sum = sum + (num / den);
}
System.out.println("Sum=" + sum);
}
}
Variable Description Table
Program Explanation
Output
Related Questions
Write a program to display the Mathematical Table from 5 to 10 for 10 iterations in the given format:
Sample Output: Table of 5
5*1 = 5
5*2 =10
--------
--------
5*10 = 50Write two separate Java programs to generate the following patterns using iteration (loop) statements:
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5Write a program to accept any 20 numbers and display only those numbers which are prime.
Hint: A number is said to be prime if it is only divisible by 1 and the number itself.
Write two separate Java programs to generate the following patterns using iteration (loop) statements:
*
* #
* # *
* # * #
* # * # *