

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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("\nFractional part1: %s \n",fractionToDecimal(n, d)); printf("enter numerator2 and denominator2:"); scanf("%d%d",&n,&d); printf("\nn = %d, d = %d ", n, d); printf("\nFractional part2: %s\n ",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 Questions & Answers
- C program to represent the numbers in spiral pattern
- 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)
- Find ΔX which is added to numerator and denominator both of fraction (a/b) to convert it to another fraction (c/d) in C++
- Represent Int64 as a String in C#
- Represent Int32 as a String in C#
- Represent Int32 as a Binary String in C#
- Represent Int64 as a Binary string in C#
- Represent Int32 as a Hexadecimal String in C#
- Represent Int32 as a Octal String in C#
- Represent Int64 as a Octal string in C#
- Represent Int64 as a Hexadecimal String in C#
- C++ program to count minimum number of binary digit numbers needed to represent n
- C Program to represent a multiplication table.
- C++ Program to Represent Linear Equations in Matrix Form
- C program to represent the alphabets in spiral pattern
Advertisements