C++ Arithmetical Operations

C++Install CompilerC++ Basics → Arithmetical Operations

C++ Arithmetic. How to add, subtract, multiply and divide numbers in C Plus Plus.

Adding two numbers

#include <iostream>
using namespace std;
int main()
{
int a = 8;
int b = 4;
int c = a + b;
cout << c << endl;
return 0;
}

NOTE: No ” ” with integers. We use ” ” only for strings.

Subtracting two numbers

#include <iostream>
using namespace std;
int main()
{
int a = 8;
int b = 4;
int c = a - b;
cout << c << endl;
return 0;
}

Multiplication of two numbers

#include <iostream>
using namespace std;
int main()
{
int a = 8;
int b = 4;
int c = a * b;
cout << c << endl;
return 0;
}

Division of two numbers

#include <iostream>
using namespace std;
int main()
{
int a = 8;
int b = 4;
int c = a / b;
cout << c << endl;
return 0;
}

Addition, Subtraction, Multiplication and Division in one program

#include <iostream>
using namespace std;
int main()
{
int a = 8;
int b = 4;
int c = a + b;
int d = a - b;
int e = a * b;
int f = a / b;
cout << c << endl;
cout << d << endl;
cout << e << endl;
cout << f << endl;
return 0;
}

Betterment in the above program

#include <iostream>
using namespace std;
int main()
{
int a = 8;
int b = 4;
int c = a + b;
int d = a - b;
int e = a * b;
int f = a / b;
cout << "Addition = " << c << endl;
cout << "Subtraction = " << d << endl;
cout << "Multiplication = " << e << endl;
cout << "Division = " << f << endl;
return 0;
}

Decimals (Fractional) Values
Integers do NOT take decimal (fractional) values. If we want to perform arithmetical operation of fractional values; we can take ‘float’ as variables.

Arithmetic of Decimals

#include <iostream>
using namespace std;
int main()
{
float a = 3.2;
float b = 4.5;
float c = a + b;
cout << c << endl;
return 0;
}

Some more basic C++ programs

#include <iostream>
using namespace std;
int main()
{
int a = 3;
int b = a + 4;
cout << b << endl;
return 0;
}

#include <iostream>
using namespace std;
int main()
{
int a = 3;
int b = a + 4;
cout << 5*b + b - 10 << endl;
return 0;
}

NEXT: Input in C++