How to do Basic Mathematical operation using C++?

Today I'm going to discuss about how can you solve basic mathematical problems like addition, subtraction, multiplication and division using C++. Our main objective is that, our program will ask for two numbers as input and it will execute the result as output. Okk? This is also a basic program of CPP.For addition the code snippets may look like this-



#include<iostream>

using namespace std;

int main()
{
    double x,y;
    
    cout<<"Enter the value of x="<<endl;
    cin>>x; //Takes the input

    cout<<"Enter the value of y="<<endl;
    cin>>y; //Takes the input
    
    cout<<"The result is="<<x+y<<endl;//Change this line
    
    

    return 0;
    }




Here the 'cout' tells the compiler what to show as the output. When you compile and run the program, first it'll ask you the value of 'x'. The 'cin' will take the value of 'x' as an input. Then the second 'cout' will execute and will ask for the value of 'y'. The 'cin' will now take the value of 'y' as input. And the last line will execute the result.

If you run the program, then it'll show somthing like this-

Enter the value of x=
2
Enter the value of y=
5
The result is=7


You can calculate the subtraction,multiplication or devision similar way.
For subtraction just change the line-
cout<<"The result is="<<x-y<<endl;
    
Take a closer look.I have changed the x+y to x-y. Now compile and run the program.The output may look something like this-
Enter the value of x=
5
Enter the value of y=
2
The result is=3


GOT IT??? For Division or Multiplication all you need to do is change the sign of X and Y. For Multiplication change it to-
cout<<"The result is="<<x*y<<endl;
    
For division change it to-
cout<<"The result is="<<x/y<<endl;
    
I'll describe more about C and Cpp programs in my next posts. Untill then "See ya"