Basic elements of C++ programs

     

A simple C++ program

//************************************
// This program prints Hello World
//      Written by: George Bebis
//      Date:       September 2, 1997
//************************************

#include <iostream.h>

main()
{
 cout << "Hello World" ;
}

      A C++ program to add two numbers

     

Program revised - Getting input from the user

#include <iostream.h>

main()
{
 int x, y, sum;

 cout << "Give the first number ";
 cin >> x;            // >> is called the extraction operator 
 cout << "Give the second number ";
 cin >> y;
 sum=x+y;
 cout << "The sum is " << sum;
}

Program revised - Using functions