C exercises and solutions: C structure add fractions

C structure: add fractions exercise

Write a C program to add two fractions and display the result fraction. Your program will prompt the user to input fraction 1 and fraction 2. The numerator and denominator of each fraction are input separately by space.  See the example output below:

  Enter fraction 1(numerator denominator): 1 2
  Enter fraction 2(numerator denominator): 2 5
  Result: 9/10 


typedef struct
        int num;
        int deno;
   Fract sum(Fract,Fract);
   int main()
     int num1,deno1,num2,deno2;
     printf("Enter fraction 1: numerator denominator:");
     printf("Enter fraction 2:numerator denominator:");
     Fract f1={num1, deno1}; // fraction 1
     Fract f2 ={num2, deno2};//fraction 2
     Fract result = sum(f1, f2);//sum the fractions
     printf("Result=%d/%d",result.num,result.deno);  //display the result
     return 0;
   Fract sum(Fract f1, Fract f2)
     Fract result={(f1.num * f2.deno) + (f2.num * f1.deno), f1.deno * f2.deno};
     return result;


This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.