Find Sum of the Series 0.6, 0.06, 0.006, 0.0006, …to n Terms in Java8 May 2025 | 5 min read Given the number of terms n, find the sum of the series 0.6, 0.06, 0.006, 0.0006,… up to n terms. Input: n=4 Output: 0.6666 Explanation: Sum of the series up to 4 terms: 0.6+0.06+0.006+0.0006= 0.66660 Input: n=5 Output: 0.66666 Explanation: Sum of the series up to 5 terms: 0.6+0.06+0.006+0.0006+0.00006=0.66666 Approach: Using the Geometric Progression FormulaThe Java program calculates the sum of a series where each term is 1/10 of the previous term, starting with 0.6. The sum is computed using the formula: Sn = a * (1 - rn) / (1 - r) Here:
AlgorithmStep 1: Assign a = 0.6 (first term), r = 0.1 (common ratio), and n = 3 (number of terms). Step 2: Use Math.pow(r, n) to compute rn. Step 3: Apply the formula Sn = a * (1 - Math.pow(r, n)) / (1 - r) to calculate the sum of the series. Step 4: Insert the values of a, r, and n into the formula to find the sum. Step 5: Print the sum along with the values of a, r, and n, formatted to six decimal places. ImplementationOutput: Sum of the series with a=0.60, r=0.10, n=3 = 0.666000 Approach: Using a Loop to Calculate the SumThe approach uses a loop to iteratively calculate the sum of the series. In each iteration, the current term is added to the sum, and the term is updated by multiplying it with the common ratio to calculate the next term. AlgorithmStep 1: Set the values for n (number of terms), a (first term), and r (common ratio) in the series. Step 2: Initialize sum = 0.0 and term = a (the first term). Step 3: Use a for loop to iterate n times. In each iteration, add the current term to the sum and multiply the term by the common ratio r to compute the next term. Step 4: Inside the loop, update the term by multiplying it by r. Step 5: After the loop completes, output the sum of the series with 6 decimal places. ImplementationFile Name: SeriesSumCalculator.java Output: Sum of the series (using loop) = 0.666000 Approach: Using RecursionThe approach used in the code is a recursive method to calculate the sum of a geometric series. It repeatedly calls itself, reducing the number of terms (n) by one in each call, until it reaches the base case where only the first term is left. AlgorithmStep 1: Define the values for the number of terms n, the initial term a, and the common ratio r in the sequence. Step 2: Call the recursive function calculateSum(a, r, n) to determine the overall sum of the series. Step 3: In the recursive function, verify if n equals 1. If accurate, yield the value of a, since only the initial term persists. Step 4: If n > 1, return the addition of the current term a and make a recursive call to calculateSum(a * r, r, n - 1) to determine the sum of the following terms. Step 5: After the recursion finishes, display the total of the series with a precision of 6 decimal points. ImplementationFile Name: SeriesSumCalculator,java Output: Sum of the series (using recursion) = 0.666000 Next TopicJava IdentityHashMap Class |
Image Processing in Java: Get and Set Pixels
Image processing is a critical aspect of computer graphics and vision, involving the manipulation and analysis of images to extract valuable information or enhance their quality. Java, with its robust libraries and straightforward syntax, provides powerful tools for image processing. One fundamental aspect of image...
6 min read
Hollow Diamond Pattern in Java
There are many pattern programs are written in Java by programmers for coding practice and cracking interviews. The pattern programs are usually asked in interviews to check the logical thinking and its implementation in program. In this section, we will create Java programs to print hollow...
4 min read
Java Program to Find the Number of Provinces
The "Number of Provinces" problem involves finding connected groups of cities represented as nodes in an undirected graph. A city group, or province, includes cities directly or indirectly connected. This Java program uses algorithms like Depth-First Search (DFS) or Union-Find to identify and count these connected...
13 min read
How many days required to learn Java
Java programming is one of the mostly used programming languages. In the IT world, there are more than 8 million Java developers. The count of Java developers is rapidly growing. Learning Java doesn't make it possible to happen overnight, and it takes time and practice. It...
5 min read
Kth Smallest Product of Two Sorted Arrays in Java
Given two sorted integer arrays, nums1 and nums2, and an integer k. The task is to determine the kth (-based) least product of nums1[i] * nums2[j], where 0 <= i < nums1.length and 0 <= j < nums2.length. Example 1: Input: nums1 = [2,8], nums2 = [3,4,5], k =...
6 min read
Print all Palindromic Levels of a Binary Tree in Java
Given a Binary Tree, the task is to print every palindromic level in this tree. Palindrome Level Any level of a binary tree is considered palindromic if it gives the same result when traversed from left to right as when traversed from right to left. Example 1: Input: ...
7 min read
Program to find the mid-point of a line in Java
Determine the midpoint of a line given two coordinates, with the beginning being (x1, y1) and the ending being (x2, y2). The Midpoint Formula The point M determined by the following formula is the midpoint of two points, (x1, y2) and (x2, y2): M = ( (x1+x2)/2,...
2 min read
DecimalFormat getMaximumIntegerDigits() method in Java
One of the java.text's built-in method is getMaximumIntegerDigits(). The Java class DecimalFomrat is used to determine the maximum number of digits that can be included in a number's integral portion. The portion of a number that appears before the decimal point (.) is known as the...
2 min read
Heart Pattern in Java
Heart Pattern is another complex pattern program that is rarely asked by the interviewers because of its complexity. We print two types of heart patterns, i.e., a simple heart and some text inside the heart. We take the help of the Math class and the lineSeparator() method...
6 min read
Object Life Cycle in Java
Based on the idea of object-oriented programming, or OOP, Java is a flexible and popular programming language. Everything in Java is an object, and objects go through many stages in their lifetime. In order to ensure proper resource management and program functioning, Java developers need to...
4 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India