Print first N terms of series (0.25, 0.5, 0.75, …) in fraction representation
Given an integer N, the task is to print the first N terms of the series in their fraction form i.e.
1/4, 1/2, 3/4, 1, 5/4, …
The above series has values as 0.25, 0.5, 0.75, 1, 1.25, ….etc. It is an Arithmetic progression that begins with 0.25 and has a difference of 0.25.
Examples:
Input: N = 6
Output: 1/4 1/2 3/4 1 5/4 3/2Input: N = 9
Output: 1/4 1/2 3/4 1 5/4 3/2 7/4 2 9/4
Approach: Consider the first four terms of the series as the base terms. Store the numerator elements and denominator elements separately.
Consider the first term 1/4, the fifth term is 1 + (1 * 4) / 4 which is 1/5.
Similarly, consider the second term 1/2 the sixth term is 1 + (1 * 2) / 2 which is 3/2.
Hence, we can consider the denominators will always be either 2, 4 or no denominator and the numerator of the term can be calculated from the denominator.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to print the required series void printSeries(int n) { // Numerators for the first four numerators // of the series int nmtr[4] = { 1, 1, 1, 3 }; // Denominators for the first four denominators // of the series int dntr[4] = { 0, 4, 2, 4 }; for (int i = 1; i <= n; i++) { // If location of the term in the series is // a multiple of 4 then there will be no denominator if (i % 4 == 0) cout << nmtr[i % 4] + (i / 4) - 1 << " "; // Otherwise there will be denominator else { // Printing the numerator and the denominator terms cout << nmtr[i % 4] + ((i / 4) * dntr[i % 4]) << "/" << dntr[i % 4] << " "; } } } // Driver code int main() { int n = 9; printSeries(n); return 0; } |
Java
// Java implementation of the approach class GFG { // Function to print the required series public static void printSeries(int n) { // Numerators for the first four numerators // of the series int[] nmtr = new int[]{ 1, 1, 1, 3 }; // Denominators for the first four denominators // of the series int[] dntr = new int[]{ 0, 4, 2, 4 }; for (int i = 1; i <= n; i++) { // If location of the term in the series is // a multiple of 4 then there will be no denominator if (i % 4 == 0) System.out.print( nmtr[i % 4] + (i / 4) - 1 + " "); // Otherwise there will be denominator else { // Printing the numerator and the denominator terms System.out.print( nmtr[i % 4] + ((i / 4) * dntr[i % 4]) +"/" + dntr[i % 4] +" "); } } } // Driver code public static void main(String[] args) { int n = 9; printSeries(n); } } // This code is contributed // by 29AjayKumar |
Python3
# Python 3 implementation of the approach # Function to print the required series def printSeries(n): # Numerators for the first four # numerators of the series nmtr = [1, 1, 1, 3] # Denominators for the first four # denominators of the series dntr = [0, 4, 2, 4] for i in range(1, n + 1, 1): # If location of the term in the # series is a multiple of 4 then # there will be no denominator if (i % 4 == 0): print(nmtr[i % 4] + int(i / 4) - 1, end = " ") # Otherwise there will be denominator else: # Printing the numerator and # the denominator terms print(nmtr[i % 4] + (int(i / 4) * dntr[i % 4]), end = "") print("/", end = "") print(dntr[i % 4], end = " ") # Driver code if __name__ == '__main__': n = 9 printSeries(n) # This code is contributed by # Shashank_Sharma |
C#
// C# implementation of the approach using System; class GFG { // Function to print the required series static void printSeries(int n) { // Numerators for the first four numerators // of the series int[] nmtr = { 1, 1, 1, 3 }; // Denominators for the first four denominators // of the series int[] dntr = { 0, 4, 2, 4 }; for (int i = 1; i <= n; i++) { // If location of the term in the series is // a multiple of 4 then there will be no denominator if (i % 4 == 0) Console.Write((nmtr[i % 4] + (i / 4) - 1) + " "); // Otherwise there will be denominator else { // Printing the numerator and the denominator terms Console.Write((nmtr[i % 4] + ((i / 4) * dntr[i % 4])) + "/" + dntr[i % 4] + " "); } } } // Driver code public static void Main() { int n = 9; printSeries(n); } } // This code is contributed // by Akanksha Rai |
1/4 1/2 3/4 1 5/4 3/2 7/4 2 9/4
Recommended Posts:
- Program to print the series 2, 1, 4, 3, 6, 5, .... up to N terms
- Find n terms of Fibonacci type series with given first two terms
- Find ΔX which is added to numerator and denominator both of fraction (a/b) to convert it to another fraction (c/d)
- Sum of the series 5+55+555+.. up to n terms
- Sum of the first N terms of the series 2, 6, 12, 20, 30....
- Sum of first n terms of a given series 3, 6, 11, .....
- Sum of the first N terms of the series 2,10, 30, 68,....
- Sum of the first N terms of the series 5,12, 23, 38....
- Sum of series 2/3 - 4/5 + 6/7 - 8/9 + ------- upto n terms
- Sum of the series 1 / 1 + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + - - - - - - upto n terms.
- Find the sum of n terms of the series 1,8,27,64 ....
- Find the sum of first N terms of the series 2×3 + 4×4 + 6×5 + 8×6 + ...
- Sum of the series 0.7, 0.77, 0.777, ... upto n terms
- Sum of the series 0.6, 0.06, 0.006, 0.0006, ...to n terms
- Find the sum of all the terms in the n-th row of the given series
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



