RAVIANS COLLEGE OF SCIENCE PATTOKI: Question No.9

Question No.9

9. If you have two fractions, a/b and c/d, their sum can be obtained from the formula
a c a*d + b*c
--- + --- = -----------
b d b*d



For example, 1/4 plus 2/3 is
1 2 1*3 + 4*2 3 + 8 11
--- + --- = ----------- = ------- = ----
4 3 4*3 12 12



Write a program that encourages the user to enter two fractions, and then displays their sum in
fractional form. (You don’t need to reduce it to lowest terms.) The interaction with the user might
look like this:

Enter first fraction: 1/2
Enter second fraction: 2/5
Sum = 9/10


You can take advantage of the fact that the extraction operator (>>) can be chained to read in more
than one quantity at once:
cin >> a >> dummychar >> b;
ANSWER BY WAJID

#include
#include

void main(void)
{
int first[2], last[2];
char operation; //In this sample programme not needed but generally for error detection only!
do{
cout<<"Enter first fraction: "; cin >>first[0]>>operation>>last[0]; //if (operation != '/') {raise error event}
cout<<"Enter second fraction: "; cin >>first[1]>>operation>>last[1]; //if (operation != '/') {raise error event}
cout<<"Sum = "<<(first[0]*last[1] + last[0]*first[1])< cout<<"\n !Press c to continue or any key to exit."< }while(getch()=='c');
}

No comments:

Post a Comment

Copyright © RAVIANS COLLEGE OF SCIENCE PATTOKI Urang-kurai