Thursday, April 15, 2021
Home C / C++ Fractional Calculator in C++

Fractional Calculator in C++

We can easily use structures to create a fractional calculator than can solve fractional numbers in C++. It is just an easy approach through structures, we can easily create a simple calculator in C++, but for a fractional calculator it is a little bit different approach.  The code of our fractional calculator is given below:

  1. // Fractional Calculator in C++
  2. #include<iostream>
  3. using namespace std;
  5. struct fraction
  6. {
  7.     int n,d;
  8.     int n1,d1;
  9. };
  11. int main()
  12. {
  13.     char p;
  14.     fraction opr;
  16.     cout<<“enter nomerator of first fraction : “;     cin>>opr.n;
  17.     cout<<endl;
  18.     cout<<“enter demonerator of first fraction”;     cin>>opr.d;
  19.     cout<<endl;
  20.     cout<<“enter operation : “;     cin>>p;
  21.     cout<<endl;
  23.     cout<<“enter nomerator of second fraction : “;     cin>>opr.n1;
  24.     cout<<endl;
  25.     cout<<“enter denomerator of second fraction : “;     cin>>opr.d1;
  26.     cout<<endl;
  28.     if(p==‘+’)
  29.     {
  30.         int z = (opr.* opr.d1 + opr.* opr.n1 );
  31.         int y = (opr.* opr.d1);
  32.         cout<<z<<“/”<<y<<endl;
  34.     }
  36.     if(p==‘-‘)
  37.     {
  38.         int a = (opr.* opr.d1  opr.* opr.n1 );
  39.         int b = (opr.* opr.d1);
  40.         cout<<a<<“/”<<b<<endl;
  42.     }
  43.     return 0;
  44. }

Now lets explain the above code of Fractional Calculator. First of all We defined a structure named as “fraction” which contains the variables for numerators and denominators in it, then we used it in the main() function by defining a variable of type “ fraction ”. Then we asked the user to enter the numbers and an operator to be performed by the program, we simply used “ if “ statements for performing the arithmetic operations, and hence it is our fractional calculator. So i think creating a calculator in C++ in not a big deal, we just have to use simplest conditional operators.

- Advertisment -

Most Popular

Android Webview Tutorial With Example | How to Use Webview in Android

Android Webview Tutorial With Example Have you seen an Android app displaying Web content using Webview. This Android Webview Tutorial is about integrating Webview using Android Studio....

Student Registration Project in PHP

In this Student Registration Project we are providing a form in simple html to the student and that form sends the data to "student.php"...

Java GUI Calculator Source Code

Here we have a Java GUI Calculator Source Code for you in which a Java GUI calculator has been created using "swing" and "awt". The calculator has...

Numeric Array in PHP

Numeric arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero....