
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Write a C program to reduce any fraction to least terms using while loop
Reducing the fraction to the lowest terms means that there is no number, except 1, that can be divided evenly into both the numerator and the denominator.
For example, 24/4 is a fraction, the lowest term for this fraction is 6, or 12/16 is a fraction the lowest term is 3/4.
Now let’s write a c program to reduce the fraction into their lowest terms.
Example 1
#include<stdio.h> int main(){ int x,y,mod,numerat,denomi,lessnumert,lessdenomi; printf("enter the fraction by using / operator:"); scanf("%d/%d", &x,&y); numerat=x; denomi=y; switch(y){ case 0:printf("no zero's in denominator
"); break; } while(mod!=0){ mod= x % y; x=y; y=mod; } lessnumert= numerat/x; lessdenomi=denomi/x; printf("lowest representation of fraction:%d/%d
",lessnumert,lessdenomi); return 0; }
Output
enter the fraction by using / operator:12/24 lowest representation of fraction:1/2
Example
//reduce the Fraction #include <stdio.h> int main() { int num1, num2, GCD; printf("Enter the value for num1 /num2:"); scanf("%d/%d", &num1, &num2); if (num1 < num2){ GCD = num1; } else { GCD = num2; } if (num1 == 0 || num2 == 0){ printf("simplified fraction is %s
", num1?"Infinity":"0"); } while (GCD > 1) { if (num1 % GCD == 0 && num2 % GCD == 0) break; GCD--; } printf("Final fraction %d/%d
", num1 / GCD, num2 / GCD); return 0; }
Output
Enter the value for num1 /num2:28/32 Final fraction 7/8
- Related Articles
- Write a C program to calculate the average word length of a sentence using while loop
- Write a C program to print ‘ABCD’ repeatedly without using loop, recursion and any control structure
- C program to find palindrome number by using while loop
- C program to print number series without using any loop
- C program to write all digits into words using for loop
- How to write a while loop in a JSP page?
- Java Program to print Number series without using any loop
- Java Program to Find Sum of Natural Numbers Using While Loop
- Java program to calculate the factorial of a given number using while loop
- do…while loop vs. while loop in C/C++
- C++ Program to Display Alphabets (A to Z) using loop
- Reduce the fraction to its lowest form in C++
- Java program to print the fibonacci series of a given number using while loop
- Java Program to Compute the Sum of Numbers in a List Using While-Loop
- How to convert a Python for loop to while loop?

Advertisements