
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Add Two Numbers
Addition is a basic arithmetic operation. The program to add two numbers performs addition of two numbers and prints their sum on screen.
A program that demonstrates addition of two numbers is given as follows −
Example
#include <iostream> using namespace std; int main() { int num1=15 ,num2=10, sum; sum = num1 + num2; cout<<"Sum of "<<num1<<" and "<<num2<<" is "<<sum; return 0; }
Output
Sum of 15 and 10 is 25
In the above program the sum of two numbers i.e. 15 and 10 is stored in variable sum.
sum = num1 + num2;
After that it is displayed on screen using the cout object.
cout<<"Sum of "<<num1<<" and "<<num2<<" is "<<sum;
A program that uses an array to store two numbers and their sum as well is given as follows −
Example
#include <iostream> using namespace std; int main() { int a[3]; a[0]=7; a[1]=5; a[2]=a[0] + a[1]; cout<<"Sum of "<<a[0]<<" and "<<a[1]<<" is "<<a[2]; return 0; }
Output
Sum of 7 and 5 is 12
In the above program, the numbers to be added are stored in the 0 and 1 index of the array.
a[0]=7; a[1]=5;
After that, the sum is stored in the 2 index of the array.
a[2]=a[0] + a[1];
The sum is displayed on screen using the cout object.
cout<<"Sum of "<<a[0]<<" and "<<a[1]<<" is "<<a[2];
- Related Articles
- Python program to add two numbers
- Java Program to Add Two Numbers
- Kotlin Program to Add two Numbers
- Java Program to Add the two Numbers
- Java Program to Add Two Complex numbers
- Haskell program to add two complex numbers
- Kotlin Program to Add Two Complex numbers
- 8051 Program to Add two 8 Bit numbers
- 8085 Program to Add two 8 Bit numbers
- 8085 program to add two 16 bit numbers
- Program to Add Two Complex Numbers in C
- How to Add two Numbers in Swift Program?
- 8085 Program to Add two multi-byte BCD numbers
- 8086 program to add two 8 bit BCD numbers
- Program to Add two multi-byte numbers in 8085 Microprocessor

Advertisements