Check out example codes for "c++ sum of all numbers up to a number". It will help you in understanding the concepts better.

Code Example 1

// Method 1: Mathematical
int sum(int n){
	return (n * (n+1)) / 2;
}

// Method 2: Using a for-loop
int sum(int n){
	int tempSum = 0; 
  	for (int i = n; i > 0; i--){
     	tempSum += i;  
    }
  	return tempSum; 
}

// Method 3: Using recuraion
int sum(int n){
	return n > 0 ? n + sum(n-1) : 0; 
}

Learn ReactJs, React Native from akashmittal.com