C++ Program to simplify fractional form in C++
Outputs:
Code:
#include <iostream>
using namespace std;
int main()
{
int num;
int denom;
char sign;
cout << "Enter fraction like 100/2: ";
cin >> num >> sign >> denom;
if (sign != '/')
exit(0); // This program is for division Only
for (int i = denom; i>=2; i--)
{
if (num % i == 0 && denom % i == 0)
{
num = num/i;
denom = denom/i;
}
}
cout << "Simplified: ";
if (denom == 1)
cout << num;
else
cout << num << "/" << denom;
cout << endl;
return 0;
}
Outputs:
Code:
#include <iostream>
using namespace std;
int main()
{
int num;
int denom;
char sign;
cout << "Enter fraction like 100/2: ";
cin >> num >> sign >> denom;
if (sign != '/')
exit(0); // This program is for division Only
for (int i = denom; i>=2; i--)
{
if (num % i == 0 && denom % i == 0)
{
num = num/i;
denom = denom/i;
}
}
cout << "Simplified: ";
if (denom == 1)
cout << num;
else
cout << num << "/" << denom;
cout << endl;
return 0;
}
No comments:
Post a Comment