C++ Code Examples: Arithmetic Mean, Sum, Product, Square
Classified in Computers
Written on in English with a size of 1.55 KB
Arithmetic Mean
The following C++ code calculates the arithmetic mean of numbers from 1 to n:
int main() {
int n;
double suma = 0;
cout << "Vnesi broj: ";
cin >> n;
if (n <= 0) {
cout << "Brojot na elementi mora da bide pogolem od 0!" << endl;
return 1;
}
for (int i = 1; i <= n; i++) {
suma += i;
}
double sredina = suma / n;
cout << "Aritmetichkata sredina na broevite od 1 do " << n << " e: " << sredina << endl;
return 0;
}
Sum
The following C++ code calculates the sum of numbers from 1 to n:
int main() {
int n, sum = 0;
cout << "Vnesi broj n: ";
cin >> n;
for (int i = 1; i <= n; i++)
... Continue reading "C++ Code Examples: Arithmetic Mean, Sum, Product, Square" »