Structure of a cpp program

# include <iostream> //standard I/O library in cpp
using namespace std; //container for names
int main(){
	cout << "Hello world!" << endl;
	cout << "Enter an integer" << endl;
	cin >> value; //input
	cout << "The integer is: " << value << endl;
	//endl is end of line/new line 
	
	return 0;
}

When you try to run a C++ program, the compiler first converts source files into object files, then links these with any required libraries and other object files, using information from header files, to produce a final executable

Functions

Program that gets the factorial of a number n:

$n! = n*(n-1)(n-2)…32*1$

int factorial (int n){
	int fact = 1;
	for(int i = 1; i<=n; i++){
		fact*=i;
	}
}

Program that swaps two variables

In C (pass by value):

void swap(int* pa, int* pb){
	//switch the addresses, not the variables 
	int temp = *pa
	*pa = *pb;
	*pb = temp;
}

int main(){
	int a= 7, b = 13;
	cout << "Before swap: a = " << a << "and b = " << b << endl;
	swap(&a,&b);
	cout << "After swap: a = " << "and b = " <<b<< endl;
	return 0;
}

In C++ (pass by reference):

void swap(int& x, int& y){ // & access original variable in main
	int temp = x;
	x = y;
	y = temp;
}

int main(){
int a = 7, b = 13;
swap(a,b);
return 0;
}