C program to represent numbers in numerator and denominator in string format

In C, we can convert fractions to their decimal string representation using dynamic memory allocation. This program handles both terminating and repeating decimal fractions, representing repeating decimals with parentheses.

Syntax

char* fractionToDecimal(int numerator, int denominator);

Algorithm

The algorithm works by performing long division and tracking remainders to detect repeating cycles −

  • Calculate the integer part using division
  • Use the remainder to compute decimal digits
  • Store each remainder to detect when a cycle begins
  • Mark repeating portions with parentheses

Example

Following is the C program to represent fractions in decimal string format −

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* fractionToDecimal(int numerator, int denominator) {
    char *result;
    int *remainders;
    int resultSize = 100, remainderSize = 100;
    int n = 0, x = 0;
    long long num, den, quotient, remainder;
    int i, repeatStart = -1;
    int isNegative = 0;
    
    result = malloc(resultSize * sizeof(char));
    
    // Handle sign
    isNegative = ((numerator > 0 && denominator < 0) ||
                  (numerator < 0 && denominator > 0)) ? 1 : 0;
    
    num = (numerator < 0) ? -numerator : numerator;
    den = (denominator < 0) ? -denominator : denominator;
    
    // Calculate integer part
    quotient = num / den;
    remainder = num % den;
    
    if (isNegative && (quotient || remainder)) {
        result[n++] = '-';
    }
    
    n += sprintf(&result[n], "%lld", quotient);
    
    if (!remainder) {
        result[n] = '\0';
        return result;
    }
    
    result[n++] = '.';
    remainders = malloc(remainderSize * sizeof(int));
    
    while (remainder) {
        // Check for repeating remainder
        for (i = 0; i < x; i += 2) {
            if (remainders[i] == remainder) {
                repeatStart = i;
                goto done;
            }
        }
        
        // Expand arrays if needed
        if (x + 1 >= remainderSize) {
            remainderSize *= 2;
            remainders = realloc(remainders, remainderSize * sizeof(int));
        }
        
        remainders[x++] = remainder;
        remainder *= 10;
        quotient = remainder / den;
        remainders[x++] = quotient;
        remainder = remainder % den;
    }
    
    done:
    for (i = 0; i < x; i += 2) {
        if (n + 3 > resultSize) {
            resultSize *= 2;
            result = realloc(result, resultSize * sizeof(char));
        }
        
        if (repeatStart == i) {
            result[n++] = '(';
        }
        
        result[n++] = '0' + remainders[i + 1];
    }
    
    if (repeatStart != -1) {
        result[n++] = ')';
    }
    
    result[n] = '\0';
    free(remainders);
    return result;
}

int main() {
    int num1, den1, num2, den2;
    char *decimal1, *decimal2;
    
    printf("Enter numerator and denominator for first fraction: ");
    scanf("%d %d", &num1, &den1);
    
    printf("Enter numerator and denominator for second fraction: ");
    scanf("%d %d", &num2, &den2);
    
    decimal1 = fractionToDecimal(num1, den1);
    decimal2 = fractionToDecimal(num2, den2);
    
    printf("Fractional part1: %s
", decimal1); printf("Fractional part2: %s
", decimal2); free(decimal1); free(decimal2); return 0; }

Output

When the above program is executed, it produces the following result −

Enter numerator and denominator for first fraction: 3 2
Enter numerator and denominator for second fraction: 4 7
Fractional part1: 1.5
Fractional part2: 0.(571428)

Key Points

  • The program uses dynamic memory allocation to handle variable-length decimal representations
  • Repeating decimals are detected by tracking remainders during long division
  • Parentheses mark the beginning and end of repeating decimal cycles
  • Memory is properly freed to prevent memory leaks

Conclusion

This program efficiently converts fractions to decimal string format using long division. It handles both terminating and repeating decimals while properly managing dynamic memory allocation.

Updated on: 2026-03-15T14:20:01+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements