Recent Posts

9-latest-250px-course

Write a C program to Add two fractions

Write a C program to Add two fractions



Program:-     

#include <stdio.h>
int main()
{
int a, b,c,d,x,y,i,gcd;
printf(“\nEnter the numerator for 1st number : “);
scanf(“%d”,&a);
printf(“\nEnter the denominator for 1st number : “);
scanf(“%d”,&b);
printf(“\nEnter the numerator for 2nd number : “);
scanf(“%d”,&c);
printf(“\nEnter the denominator for 2nd number : “);
scanf(“%d”,&d);
x=(a*d)+(b*c); //numerator
y=b*d; //denominator
// Trick part. Reduce it to the simplest form by using gcd.
for(i=1; i <= x && i <= y; ++i)
{
if(x%i==0 && y%i==0)
gcd = i;
}
printf(“\nThe added fraction is %d/%d “,x/gcd,y/gcd);
printf(“\n”);
return 0;
}

Output:-

....
SHARE

Milan Tomic

Hi. I’m Designer of Blog Magic. I’m CEO/Founder of ThemeXpose. I’m Creative Art Director, Web Designer, UI/UX Designer, Interaction Designer, Industrial Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer. Inspired to make things looks better.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment