In c programming when I divide 2 numbers like 2/4 it gives output 0.5 but I want 1/2. So I want know how to perform division to get answer in fractions like numerator/denominator. I want in C.
Asked
Active
Viewed 9,390 times
-2
-
3Nope. In C, `2/4` gives `0` not `0.5`. As for the problem, how would you simplify the fraction on paper? You'll need to find a common divisor and then divide the numerator and denominator with it until no such divisor exists (except 1) – Spikatrix Jun 21 '17 at 06:54
-
1google `integer division`. – Sourav Ghosh Jun 21 '17 at 06:54
-
1Find HCF, divide both numbers and print in that format. – ameyCU Jun 21 '17 at 06:55
-
1Possible duplicate of [How to output fraction instead of decimal number?](https://stackoverflow.com/questions/4819075/how-to-output-fraction-instead-of-decimal-number) – Shloim Jun 21 '17 at 06:56
-
It was in c++ but I want in c #Shloim – Uchiha Lucky CS Jun 21 '17 at 06:57
-
1The question is a bit broad; perhaps you need an implementation of rational numbers. – Codor Jun 21 '17 at 06:57
-
Since there's no native solution in C++, there is non in C. You can port the C++ solution given in the link to C. – Shloim Jun 21 '17 at 07:05
-
1There's no standard library support for rational numbers (ratio of two integers) in C. What you can do is write code that uses a structure type to provide such support. You need to know about the oldest of all algorithms — the greatest common divisor or GCD, which dates back to Ancient Greece. You may decide you need an arbitrary precision integer library to support that, or you might find that there's a library that already does the job. GMP is a well-known multi-precision library. – Jonathan Leffler Jun 21 '17 at 07:59
3 Answers
2
What you really want is to reduce the fraction.. not compute the division.
Here's a quick sample that will yield the reduced fraction:
#include <stdbool.h>
#include <stdio.h>
//gcf function - return gcd of two numbers
int gcd(int n, int m)
{
int gcd, remainder;
while (n != 0)
{
remainder = m % n;
m = n;
n = remainder;
}
gcd = m;
return gcd;
}//end gcd function
int main (int argc, const char * argv[]) {
// insert code here...
//--declarations
int number1, number2;
int newNumber1, newNumber2;
//--get user input
printf("Enter a fraction: ");
scanf("%d/%d", &number1, &number2);
//--calculations
//find the gcd of numerator and denominator
//then divide both the numerator and denominator by the GCD
newNumber1 = number1 / gcd(number1, number2);
newNumber2 = number2 / gcd(number1, number2);
//--results
printf("In lowest terms: %d/%d", newNumber1, newNumber2);
}
Sample taken from: http://snipplr.com/view/42917/

Bill Tarbell
- 4,933
- 2
- 32
- 52
0
C does not have that ability built in. However, what you can do is, write a small function that takes fraction values (0.5 in your case) and returns the fraction the form your are looking for (ie, 1/2).
But remember, 1/2 is not a numeric; it is a string (char array). So, you can use it for display/print purpose only; you can not use in arithmetic expressions.

Kazhian
- 9
- 2
0
sorry that I can not adapt this to C, but you can for sure port back the code since is not soo complex
#include <iostream>
#include <string>
long GreatestCommonMultiple(long& a, long& b);
std::string SimplifyThis(long& a, long& b);
int main(int argC, char** argV)
{
long n1 = 3;
long n2 = 21;
std::cout << "This as simplified fraction: " << n1 << "/" << n2 << " is "<<SimplifyThis(n1, n2)<<std::endl;
return 0;
}
std::string SimplifyThis(long& a, long& b) {
long gcm1 = GreatestCommonMultiple(a, b);
return std::to_string(a / gcm1) + "/" + std::to_string(b / gcm1);
}
long GreatestCommonMultiple(long& a, long& b) {
if (b==0)
{
return a;
}
long x = (a % b);
return GreatestCommonMultiple(b, x);
}

ΦXocę 웃 Пepeúpa ツ
- 47,427
- 17
- 69
- 97