5 Replies - 2942 Views - Last Post: 07 October 2009 - 09:19 PM Rate Topic: -----

#1 heckyaaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 07-October 09

Multiplying and Adding Fractions in C

Posted 07 October 2009 - 09:03 AM

The object of the program is to read in 3 fractions and find both their sum and product. I am unsure how to find sum/product without first prompting for the operator, I'd be greatful for any help.

The code is atleast I believe it is reading in the intergers I put in, however the operator functions are not working the way I need them to. When I enter numbers it always gives me whatever numbers I used for numerators over 0. So not sure if it's something with my logic or it happens to be a coding error.
#include <stdio.h>
#include <conio.h>
#include <math.h>


int main()
{

int a;
int b;
int c;
int d;
int e;
int f;
float x;
float y;
char oper;

printf("Enter the first fraction as two intergers separated by a slash:");
scanf("%d/%d",&a,&b);

printf("Enter the second fraction as two integers separated by a slash:");
scanf("%d/%d",&c,&d);

printf("Enter the third fraction as two intergers separated by a slash:");
scanf("%d/%d",&e,&f);

printf("Enter an arithmetic operator( +,* ):");
scanf("%s",&oper);


switch(oper)
{

case '*':
{
x = (a)*(c)*(e);
y = (b)*(d)*(f);
printf("%d/%d\n",x,y);
};
break;

case '+':
{
x = ((a)*(d)*(f))+((c)*(b)*(f))+((e)*(b)*(d));
y = (b)*(d)*(f);
printf("%d/%d\n",x,y);
};
break;

}

getch();

return 0;
}


This post has been edited by heckyaaa: 07 October 2009 - 09:16 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Multiplying and Adding Fractions in C

#2 aks29921  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 84
  • View blog
  • Posts: 230
  • Joined: 24-August 09

Re: Multiplying and Adding Fractions in C

Posted 07 October 2009 - 10:40 AM

View Postheckyaaa, on 7 Oct, 2009 - 08:03 AM, said:

The object of the program is to read in 3 fractions and find both their sum and product. I am unsure how to find sum/product without first prompting for the operator, I'd be greatful for any help.

The code is atleast I believe it is reading in the intergers I put in, however the operator functions are not working the way I need them to. When I enter numbers it always gives me whatever numbers I used for numerators over 0. So not sure if it's something with my logic or it happens to be a coding error.
#include <stdio.h>
#include <conio.h>
#include <math.h>


int main()
{

int a;
int b;
int c;
int d;
int e;
int f;
float x;
float y;
char oper;

printf("Enter the first fraction as two intergers separated by a slash:");
scanf("%d/%d",&a,&b);

printf("Enter the second fraction as two integers separated by a slash:");
scanf("%d/%d",&c,&d);

printf("Enter the third fraction as two intergers separated by a slash:");
scanf("%d/%d",&e,&f);

printf("Enter an arithmetic operator( +,* ):");
scanf("%s",&oper);


switch(oper)
{

case '*':
{
x = (a)*(c)*(e);
y = (b)*(d)*(f);
printf("%d/%d\n",x,y);
};
break;

case '+':
{
x = ((a)*(d)*(f))+((c)*(b)*(f))+((e)*(b)*(d));
y = (b)*(d)*(f);
printf("%d/%d\n",x,y);
};
break;

}

getch();

return 0;
}


you have got your fractions wrong....
get the input from the user as numerator and denominator separately
if a & b are the values entered respectively, then your fraction will be a/b (similarly c/d & e/f after getting c,d,e,f from user wll be your second and third fractions)
('/' is the arithmetic operation of division)
now perform the arithmetic calculations like (a/B)*(c/d) (for multiplying the two fractions) etc.

This post has been edited by aks29921: 07 October 2009 - 10:44 AM

Was This Post Helpful? 1
  • +
  • -

#3 heckyaaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 07-October 09

Re: Multiplying and Adding Fractions in C

Posted 07 October 2009 - 12:47 PM

This is what I got and I can't figure out how to output both the sum and the product in the correct notations? Any suggestions?

#include <stdio.h>
#include <conio.h>
#include <math.h>


int main()
{

int Num1;
int Den1;
int Num2;
int Den2;
int Num3;
int Den3;
float x;
float y;
float sum1;
float sum2;
char oper;

printf("Enter the numerator for the first fraction:");
scanf("%i", &Num1);

printf("Enter the denominator for the first fraction:");
scanf("%i", &Den1);

printf("Enter the numerator for the second fraction:");
scanf("%i", &Num2);

printf("Enter the denominator for the second fraction:");
scanf("%i", &Den2);

printf("Enter the numerator for the third fraction:");
scanf("%i", &Num3);

printf("Enter the denominator for the third fraction:");
scanf("%i", &Den3);

x = (Num1 * Num2 * Num3);
y = (Den1 * Den2 * Den3);

printf("The Product is: %x/%y\n", &x,&y);

sum1 = (Num1 * Den2 * Den3) + (Num2 * Den1 * Den3) + ( Num3 * Den1 * Den2);
sum2 = (Den1 * Den2 * Den3);
printf("The Sum of the fractions is: (%Num1 * %Den2 * %Den3) + (%Num2 * %Den1 * %Den3) + (%Num3 * %Den1 * %Den2)/(%Den1 * %Den2 * %Den3) = (%sum1 / %sum2)\n", &Num1,&Den1,&Num2,&Den2,&Num3,&Den3,sum1,sum2);

getch();

return 0;
}


Was This Post Helpful? 0
  • +
  • -

#4 aks29921  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 84
  • View blog
  • Posts: 230
  • Joined: 24-August 09

Re: Multiplying and Adding Fractions in C

Posted 07 October 2009 - 01:00 PM

Quote

x = (Num1 * Num2 * Num3);
y = (Den1 * Den2 * Den3);

printf("The Product is: %x/%y\n", &x,&y);

sum1 = (Num1 * Den2 * Den3) + (Num2 * Den1 * Den3) + ( Num3 * Den1 * Den2);
sum2 = (Den1 * Den2 * Den3);
printf("The Sum of the fractions is: (%Num1 * %Den2 * %Den3) + (%Num2 * %Den1 * %Den3) + (%Num3 * %Den1 * %Den2)/(%Den1 * %Den2 * %Den3) = (%sum1 / %sum2)\n", &Num1,&Den1,&Num2,&Den2,&Num3,&Den3,sum1,sum2);

try:
x = (Num1 * Num2 * Num3);
y = (Den1 * Den2 * Den3);
printf("The Product is: %f/%f\n", x,y);


similarly for the sum..
Was This Post Helpful? 0
  • +
  • -

#5 heckyaaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 07-October 09

Re: Multiplying and Adding Fractions in C

Posted 07 October 2009 - 01:18 PM

That worked just slick. Now it's time to format the program correctly. I'm supposed to read in the fractions, display them then show the fractions adding/multiplying and printf the sum and product.

Is there a way to backspace over what has been printed out?

This is the code so far, getting really close, thanks for all the help aks29921. I really appreciate it.
#include <stdio.h>
#include <conio.h>
#include <math.h>


int main()
{

int Num1;
int Den1;
int Num2;
int Den2;
int Num3;
int Den3;
int x;
int y;
int sum1;
int sum2;
char oper;

printf("Enter the numerator for the first fraction:");
scanf("%i", &Num1);

printf("Enter the denominator for the first fraction:");
scanf("%i", &Den1);

printf("Enter the numerator for the second fraction:");
scanf("%i", &Num2);

printf("Enter the denominator for the second fraction:");
scanf("%i", &Den2);

printf("Enter the numerator for the third fraction:");
scanf("%i", &Num3);

printf("Enter the denominator for the third fraction:");
scanf("%i", &Den3);

printf("Enter the First fraction: %i/%i\n", Num1,Den1);
printf("Enter the Second Fraction: %i/%i\n", Num2,Den2);
printf("Enter the Third Fraction: %i/%i\n", Num3,Den3);

x = (Num1 * Num2 * Num3);
y = (Den1 * Den2 * Den3);
printf("The Product is: %d/%d\n", x,y);


sum1 = (Num1 * Den2 * Den3) + (Num2 * Den1 * Den3) + ( Num3 * Den1 * Den2);
sum2 = (Den1 * Den2 * Den3);
printf("The Sum of the fractions is:%d/%d\n", sum1,sum2);

getch();

return 0;
}


This post has been edited by heckyaaa: 07 October 2009 - 03:18 PM

Was This Post Helpful? 0
  • +
  • -

#6 heckyaaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 07-October 09

Re: Multiplying and Adding Fractions in C

Posted 07 October 2009 - 09:19 PM

Finished code..
#include <stdio.h>
#include <conio.h>		/* Borland Compiler Only */
#include <math.h>

int main()
{

 int Num1;			   /* Numerator for the first fraction */
 int Den1;			   /* Denominator for the first fraction */
 int Num2;			   /* Numerator for the second fraction */
 int Den2;			   /* Denominator for the second fraction */
 int Num3;			   /* Numerator for the third fraction */
 int Den3;			   /* Denominator for the third fraction */
 int x;				  /* Numerator placeholder of the fraction in the product */ 
 int y;				  /* Denominator placeholder of the fraction in the product */
 int sum1;			   /* Numerator placeholder for the sum */ 
 int sum2;			   /* Denominator placeholder for the sum */
 
						 /* Prompts for fractions to be entered */
	 printf("Enter the numerator for the first fraction:");
	 scanf("%i", &Num1);
	 printf("Enter the denominator for the first fraction:");
	 scanf("%i", &Den1);
	 printf("Enter the numerator for the second fraction:");
	 scanf("%i", &Num2);
	 printf("Enter the denominator for the second fraction:");
	 scanf("%i", &Den2);
	 printf("Enter the numerator for the third fraction:");
	 scanf("%i", &Num3);
	 printf("Enter the denominator for the third fraction:");
	 scanf("%i", &Den3);

	 clrscr();		  /* Borland Compiler Only */

						/* Print out of the fractions entered */
	 printf("Enter the First fraction: %i/%i\n", Num1,Den1);
	 printf("Enter the Second Fraction: %i/%i\n", Num2,Den2);
	 printf("Enter the Third Fraction: %i/%i\n", Num3,Den3);

					   /* Computation to find sum numerator and denominator
						  followed by a printf of the operation */
	 sum1 = (Num1 * Den2 * Den3) + (Num2 * Den1 * Den3) + ( Num3 * Den1 * Den2);
	 sum2 = (Den1 * Den2 * Den3);
	 printf("%d/%d + %d/%d + %d/%d = %d/%d\n", Num1,Den1,Num2,Den2,Num3,Den3,sum1,sum2);

					  /* Computation to find the product numerator and
						 denominator followed by a printf of the operation */
	 x = (Num1 * Num2 * Num3);
	 y = (Den1 * Den2 * Den3);
	 printf("%d/%d + %d/%d + %d/%d = %d/%d\n", Num1,Den1,Num2,Den2,Num3,Den3,x,y);

					 /* System pause; hold user screen open */
	 getch();

	 return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1