In this tutorial you will learn about the C++ Program to Add Two Numbers and its application with practical example.
C++ Program to Add Two Numbers
In this tutorial, we will learn to create a C++ program that will Add Two Numbers in C++ programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:
- Operators in C++ Programming.
- Basic Input and Output function in C++ Programming.
- Basic C++ Programming.
What is addition?
The addition in mathematics is the sum of two numbers. The arithmetic operations are done using the arithmetic operators. These operators are used between two arithmetic operands or between two numbers.
Program to Add Two Numbers:-
In this tutorial, we will create a program that will add the two numbers using an addition operator. We will first take two numbers in input and then we will add them and store the value in the third variable.
As we all know the integer, float, or double data types in c++ programming. In c++ programming, we can take the input from the user in numbers format. In today’s program, we will take input in number format. Secondly, we will add the number with the help of a small program. The numbers are the most useful terminology in any programing language. For number/arithmetic manipulation in c++ programming, there are many predefined functions available.
Numbers are the most useful data types in any programming language. The integer values are used with operators to perform the best operations in programming.
With the help of this program, we can take input and Add Two Numbers.
Algorithm to Add the numbers:-
1 2 3 4 5 6 7 8 9 10 |
1. Declaring the variables for the program. 2. Taking the input numbers from the user in number format. 3. Adding the numbers and storing it in a variable. 4. End program. |
Program to Add Two Numbers:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/* C++ Program to Add Two Numbers */ #include <iostream> using namespace std; int main() { //Declaring the variables for the program int fno, sno, sum; //Taking input numbers from the user for addition of the two values. cout << "Enter two numbers: "; cin >> fno >> sno; // sum of two numbers in stored in variable sum sum = fno + sno; // Prints sum of two numbers cout << fno << " + " << sno << " = " << sum; return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- fno = it will hold the input number value from the user.
- sno = it will hold the input number value from the user.
- sum = it will hold the sum value of the numbers.
Taking Input numbers from the user.
Calculating the sum of two numbers.
Printing the output.