Read a fraction of the form a/b from the keyboard, reduce it to its lowest form, and print the answers. user enters 2/4 program prints 1/2
Details:
Prompt for a fraction, and then read the input from the keyboard.
Validate input, print a error message if invalid & allow for fraction to be re-entered.
Numerator & denominator are 32-bit signed integers.
The output must be a fraction of the form a/b. 6/3 will be printed as 2/1.
Sample Program:
enter a fraction to reduce: this isnt a fraction
Invalid Input bro.
enter a fraction to reduce: 2/10
Here is your fraction: 1/5
int GCD(int a, int b) {
if(b==0)
return a;
else
return GCD(b, a % b);
}
#include <stdio.h>
int getfraction(int n, int m)
{
int getfraction, remainder;
while (n != 0)
{
remainder = m % n;
m = n;
n = remainder;
}
getfraction = m;
return getfraction;
}
int main (int argc, const char * argv[]) {
int num1, num2;
int newNum1, newNum2;
char yesno;
for(;
{
printf("Enter a fraction you wish to reduce: ");
scanf("%d/%d", &num1, &num2);
newNum1 = num1 / getfraction(num1, num2);
newNum2 = num2 / getfraction(num1, num2);
printf("Your Fraction Reduces to: %d/%d \n", newNum1, newNum2);
printf("Do you want to reduce another fraction?(y/n): \t ");
scanf("%s", &yesno);
if (yesno == 'n'
{break;}
}}
Copyright © 2026, NextGenUpdate.
All Rights Reserved.