Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Print first N terms of series (0.25, 0.5, 0.75, ...) in fraction representation
In C programming, printing a series like (0.25, 0.5, 0.75, ...) in fraction representation involves converting decimal values to their equivalent fractions. This series represents multiples of 0.25, which can be expressed as fractions with denominator 4.
Syntax
for (i = 0; i < n; i++) {
// Calculate numerator and denominator
// Print in fraction form: numerator/denominator
}
Algorithm
START
Step 1 ? Declare variables: int num, den, i, n
Step 2 ? Input number in n
Step 3 ? Loop from i = 0 to i < n
If i % 2 == 0 (even index)
If i % 4 == 0
Set num = i/4 and den = 0 (whole number)
Else
Set num = i/2 and den = 2 (half)
Else (odd index)
Set num = i and den = 4 (quarter fraction)
If den != 0
Print num/den
Else
Print num
Step 4 ? End Loop
STOP
Example
Here's a complete C program to print the first N terms of the series in fraction representation −
#include <stdio.h>
int main() {
int num, den;
int i, n;
printf("Enter series limit: ");
scanf("%d", &n);
printf("Series: ");
for (i = 0; i < n; i++) {
if (i % 2 == 0) {
if (i % 4 == 0) {
num = i / 4;
den = 0; // Whole number
} else {
num = i / 2;
den = 2; // Half
}
} else {
num = i;
den = 4; // Quarter
}
if (den != 0) {
printf("%d/%d ", num, den);
} else {
printf("%d ", num);
}
}
printf("<br>");
return 0;
}
Output
Enter series limit: 5 Series: 0 1/4 1/2 3/4 1
How It Works
- For even indices divisible by 4 (0, 4, 8...): Result is a whole number (i/4)
- For other even indices (2, 6, 10...): Result is i/2 with denominator 2
- For odd indices (1, 3, 5...): Result is i/4 as a proper fraction
Conclusion
This program efficiently generates fractions representing the series 0, 0.25, 0.5, 0.75, 1.0... by using modular arithmetic to determine appropriate numerators and denominators. The pattern recognition makes it suitable for any number of terms.
Advertisements
