- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C program to represent numbers in numerator and denominator in string format
Problem
Find the fraction part from two given integers given by user at run time by using the dynamic memory allocation and represent the numerator and denominator in string format.
Solution
The solution to represent the numerator and denominator in string format is as follows −
Example -
- The input is given below −
Numerator1 = 3 Denominator2 = 2 numerator2 = 4 denominator2 = 7
- The output is as follows −
Fractional part1: 1.5 Fractional part2: 0.(571428)
Example
Following is the C program to represent the numerator and denominator in string format −
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> char* fractionToDecimal(int numerator, int denominator) { char *p; int psz, n, *dec, dsz, x; long long num, den, k, f; int i, repeat_at; int neg = 0; psz = dsz = 100; n = x = 0; p = malloc(psz * sizeof(char)); //assert(p); neg = ((numerator > 0 && denominator < 0) || (numerator < 0 && denominator > 0)) ? 1 : 0; num = numerator; den = denominator; num = (num < 0) ? -num : num; den = (den < 0) ? -den : den; k = num / den; f = num % den; if (neg && (k || f)) p[n ++] = '-'; n += sprintf(&p[n], "%lld", k); if (!f) { p[n] = 0; return p; } p[n ++] = '.'; dec = malloc(dsz * sizeof(int)); repeat_at = -1; if (f < 0) f = -f; while (f) { for (i = 0; i < x; i += 2) { if (dec[i] == f) { repeat_at = i; goto done; } } if (x + 1 >= dsz) { dsz *= 2; dec = realloc(dec, dsz * sizeof(int)); } dec[x ++] = f; f *= 10; k = f / den; dec[x ++] = k; f = f % den; } done: for (i = 0; i < x; i += 2) { if (n + 3 > psz) { psz *= 2; p = realloc(p, psz * sizeof(char)); } if (repeat_at == i) { p[n ++] = '('; } p[n ++] = '0' + dec[i + 1]; } if (repeat_at != -1) p[n ++] = ')'; p[n ++] = 0; free(dec); return p; } int main(void){ int n,d; printf("enter numerator1 and denominator1:"); scanf("%d%d",&n,&d); printf("n = %d, d = %d ", n, d); printf("
Fractional part1: %s
",fractionToDecimal(n, d)); printf("enter numerator2 and denominator2:"); scanf("%d%d",&n,&d); printf("
n = %d, d = %d ", n, d); printf("
Fractional part2: %s
",fractionToDecimal(n, d)); return 0; }
Output
When the above program is executed, it produces the following result −
enter numerator1 and denominator1:4 5 n = 4, d = 5 Fractional part1: 0.8 enter numerator2 and denominator2:5 9 n = 5, d = 9 Fractional part2: 0.(5)
- Related Articles
- C++ program to represent the Fraction of Two Numbers in the String Format
- What is the Numerator and the Denominator?
- The sum of the numerator and denominator of a fraction is 3 less than twice the denominator. If the numerator and denominator are decreased by 1, the numerator becomes half the denominator. Determine the fraction.
- C program to represent the numbers in spiral pattern
- Find the equivalent fraction of ( frac{13}{7} ) havinga. numerator 52 .b. denominator 49.c. numerator 130.d. denominator 63.
- Find the equivalent fraction of ( frac{3}{5} ) having(a) denominator 20 (b) numerator 9(c) denominator 30 (d) numerator 27
- The denominator of a fraction is 4 more than twice its numerator. Denominator becomes 12 times the numerator. If both the numerator and the denominator are reduced by 6. Find the fraction.
- The numerator of a fraction is 4 less than the denominator. If the numerator is decreased by 2 and denominator is increased by 1, then the denominator is eight times the numerator. Find the fraction.
- If the Denominator Is Negative And the Numerator Is Positive in a rational number, Then How Can We Represent it on the Number Line?
- C++ program to find ΔX which is added to numerator and denominator both of fraction (a/b) to convert it to another fraction (c/d)
- The sum of the numerator and denominator of a fraction is 4 more than twice the numerator. If the numerator and denominator are increased by 3, they are in the ratio $2 : 3$. Determine the fraction
- What Is The Line Called Which Comes Between Numerator And Denominator?
- In a fraction, twice the numerator is 2 more than the denominator. If 3 is added to the numerator and denominator the new fraction is $frac{2}{3}$. Find the original fractions.
- Write the numerator and denominator of the following fraction:$frac{-9}{-6}$
- Find ΔX which is added to numerator and denominator both of fraction (a/b) to convert it to another fraction (c/d) in C++

Advertisements