< C++ Programming < Code < Variables

Variables

Adds two numbers and prints their sum

// This program adds two numbers and prints their sum.
#include <iostream>
 
int main()
{
  int a;
  int b;
  int sum;

  sum = a + b;
 
  std::cout << "The sum of " << a << " and " << b << " is " << sum << "\n";
 
  return 0;
}

// This program adds two numbers and prints their sum, variation 1
#include <iostream>
#include <ostream>

using namespace std;

int main()
{
  int a = 123, b (456), sum = a + b;
 
  cout << "The sum of " << a << " and " << b << " is " << sum << endl;
 
  return 0;
}
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.