Sample Code in C++ to Add Numbers

Adding Two or Multiple Numbers in C++ is a great way of learning how variables work in C++. This post provides C++ Code and screenshots for you to quickly learn how to use C++ and Microsoft Visual Studio to Add Two Numbers and display the result. Note that this example uses Integer variables identified by int keyword and similarly you can add float, double and other data types.

C++ Sample Add Numbers Initial Project

C++ Sample Add Numbers Initial Project

The above screenshot displays a initial code created by Application Wizard as outlined in the C++ Hello World sample Application. Now it’s time to add some code to the initial C++ Project and test the functionality. Now pay attention and have a close look to the given below screenshot which displays the code required to be added to the C++ Project to successfully add two numbers in C++. Also remember that C++ is case sensitive so int and Int are totally different words.

C++ Sample Add Numbers Final Code

C++ Sample Add Numbers Final Code

Note that the code to Add Tow Numbers in C++ is displayed in the above screenshot. The C++ Code is accompanied with comments. Once you have written C++ code to add two numbers to your project, simply compile it and run it. This example uses two integer variables, however there are many different ways in which numbers to be added are specified.

C++ Sample Add Numbers Project Running in Command Prompt Outside Visual Studio

C++ Sample Add Numbers Project Running in Command Prompt Outside Visual Studio

The above screenshot displays the Application Directory and displays how to run the application to execute the application file created. Feel free to experiment and add multiple positive and negative numbers using C++ to learn how to delegate your computing tasks to your C++ Projects. You can also download the Sample Code in C++ to Add Tow Numbers instead of typing all the text os simply copy paste from the C++ Code given below.

#include "stdafx.h"#include <iostream> // Line 1 Addedint _tmain(int argc, _TCHAR* argv[]){//Begin Codeint Number1,Number2,SumOfNumbers;Number1=100;Number2=200;SumOfNumbers=Number1+ Number2;std::cout << "Addition of Number1 and Number2 is " << SumOfNumbers;//End Codereturn 0;}