0% found this document useful (0 votes)
426 views3 pages

Project 1b

This C++ program allows users to perform arithmetic operations on fractions by prompting them to enter numerators and denominators. It defines functions to add, subtract, multiply and divide fractions, and stores the result numerator and denominator by reference. The main function gets user input for the operation and fraction values, calls the appropriate arithmetic function, checks for invalid inputs like division by zero, simplifies the result if possible, and prints the final answer.

Uploaded by

api-270149432
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
426 views3 pages

Project 1b

This C++ program allows users to perform arithmetic operations on fractions by prompting them to enter numerators and denominators. It defines functions to add, subtract, multiply and divide fractions, and stores the result numerator and denominator by reference. The main function gets user input for the operation and fraction values, calls the appropriate arithmetic function, checks for invalid inputs like division by zero, simplifies the result if possible, and prints the final answer.

Uploaded by

api-270149432
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <iostream>

using namespace std;


void manu();
int addFrac(int num1, int num2, int deno1, int deno2, int& nResult, int& dResult);
int subFrac(int num1, int num2, int deno1, int deno2, int &nResult, int &dResult);
int multiFrac(int num1, int num2, int deno1, int deno2, int &nResult, int &dResult);
int dividFrac(int num1, int num2, int deno1, int deno2, int &nResult, int &dResult);
void manu()
{//asks for inputs
int choice, num1, num2, deno1, deno2, nResult = 0, dResult = 0, i, j;
cout<<"This program allows you to perform arithmetic operations on
fractions."<<endl;
cout<<"A series of questions will allow you to pick your inputs such that it is
written in a/b and c/d"<<endl;
cout<<"Pick an arithmetic operation to perform:"<<endl;
cout<<"1. Addition"<<endl;
cout<<"2. Subtraction"<<endl;
cout<<"3. Multiplication"<<endl;
cout<<"4. Division"<<endl;
cin>>choice;
cout<<"Enter the numerator of the first fraction"<<endl;
cin>>num1;
cout<<"Enter the second numerator of the first fraction"<<endl;
cin>>num2;
cout<<"Enter the denominator"<<endl;
cin>>deno1;
cout<<"Enter the second denominator"<<endl;
cin>>deno2;
main();
}
int main()
{
//calculations according to choice picked
if(choice==1)
{
addFrac(num1, num2, deno1, deno2, nResult, dResult);
}
else if(choice==2)
{
subFrac(num1, num2, deno1, deno2, nResult, dResult);
}
else if (choice==3)
{
multiFrac(num1, num2, deno1, deno2, nResult, dResult);
}
else if (choice==4)
{
dividFrac(num1, num2, deno1, deno2, nResult, dResult);

}
if (dResult==0)//checks for undefined values
{
cout<<"Invalid input! Cannot divide by 0."<<endl;
}
else if (nResult==0)//checks for 0
{
cout<<"Answer= 0"<<endl;
}
else {
if(nResult<dResult){
i=dResult;}
else{
i=nResult;}
for(j=i; j>0; j--)
{
if(nResult%j==0 && dResult%j==0){
nResult=nResult/j;
dResult=dResult/j;
}
}
cout<<"Answer= "<<nResult<<"/"<<dResult<<endl;
}
return 0;
}
//arithematic functions
int addFrac(int num1, int num2, int deno1, int deno2, int& nResult, int& dResult)
{
nResult=num1+num2;
dResult=deno1+deno2;
return nResult;
return dResult;
}
int subFrac(int num1, int num2, int deno1, int deno2, int &nResult, int &dResult)
{
nResult=num1-num2;
dResult=deno1-deno2;
return nResult;
return dResult;
}
int multiFrac(int num1, int num2, int deno1, int deno2, int &nResult, int &dResult)
{
nResult=num1*num2;
dResult=deno1*deno2;
return nResult;

return dResult;
}
int dividFrac(int num1, int num2, int deno1, int deno2, int &nResult, int &dResult)
{
nResult=num1/num2;
dResult=deno1/deno2;
return nResult;
return dResult;
}

You might also like