Program to add two numbers in C++

By: Stanley B. Emailed: 1585 times Printed: 2039 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Using the IO library, we can extend our main program to ask the user to give us two numbers and then print their sum:

#include <iostream>
int main()
{
std::cout << "Enter two numbers:" << std::endl;
int v1, v2;
std::cin >> v1 >> v2;
std::cout << "The sum of " << v1 << " and " << v2
<< " is " << v1 + v2 << std::endl;
return 0;
}

This program starts by printing

Enter two numbers:

on the user's screen and then waits for input from the user. If the user enters

3 7

followed by a newline, then the program produces the following output:

The sum of 3 and 7 is 10

The first line of our program is a preprocessor directive:

#include <iostream>

which tells the compiler that we want to use the iostream library. The name inside angle brackets is a header. Every program that uses a library facility must include its associated header. The #include directive must be written on a single linethe name of the header and the #include must appear on the same line. In general, #include directives should appear outside any function. Typically, all the #include directives for a program appear at the beginning of the file.


C++ Home | All C++ Tutorials | Latest C++ Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(1)


1. View Comment

why can't we download & install keybasic on phone that enable java? plz work on it.

View Tutorial          By: jimoh abiodun at 2013-04-14 13:06:18

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Stanley B.
Enumerations in C++
typedef in C++
Declarations and Definitions in C++
Uninitialized Variables Cause Run-Time Problems in C++
C++ keywords
Lvalues and Rvalues in C++
Multi-Line Literals in C++
Concatenated String Literals in C++
Character String Literals in C++
Escape Sequences for Nonprintable Characters in C++
Using the Built-in Arithmetic Types in C++
if in C++
Programming errors a compiler will detect in C++
for loop in C++
while loop in C++

More Tutorials in C++
Two-Dimensional Array Manipulation in C++
Calculate average using Two-Dimensional Array in C++
Compute the square root of the sum of the squares of an array in C++
Matrix using nested for loops in C++
Sorting an array of Strings in C++
Calculating total based on the given quantity and price in C++
Compiling and Linking Multiple Source Files in C++
Enumerations in C++
Program to add two numbers in C++
Comments in C++
while loop in C++
for loop in C++
Programming errors a compiler will detect in C++
if in C++
Using the Built-in Arithmetic Types in C++

More Latest News
Most Viewed Articles (in C++ )
C++ Recursion function explained using Fibonacci series
Using cout.width() in C++
Constructors and Destructors in C++
Calculating total based on the given quantity and price in C++
Difference between Procedural, Structured, and Object-Oriented Programming
Using Comments in a C++ Program
atoi(), itoa() in C++
strcpy() and strncpy() sample program in C++
Getting Started with C++
Demonstration of Prefix and Postfix operators in C++
Use of Conditional (Ternary) Operator in C++
while (1) Loops in C++
The indirection operator (*) - dereference operator.
Operator Precedence in C++
qsort() sample program in C++
Most Emailed Articles (in C++)
Converting C++ Multiple-Inheritance Hierarchies to Java
Returning values from a function in C++
C++ and Object-Oriented Programming
Advanced if Statements in C++
do...while Loops in C++
Lvalues and Rvalues in C++
Using cout in C++
C++ Recursion function explained using Fibonacci series
Public versus Private members in C++
Converting Pointer Parameters in C++ to Java
Using cout.fill() in C++
Getting Started with C++
Using Comments in a C++ Program
The if Statement in C++
Passing parameters to a function by value in C++