Skip to main content

Functions ยป Examples

Calculate area of a circleโ€‹

double area_of_circle(double radius) {
return 3.14159 * radius * radius;
}

๐Ÿ’ก Idea: Use the M_PI constant from the <cmath> header instead of 3.14159.

Convert Fahrenheit to Celsiusโ€‹

double fahrenheit_to_celsius(double fahrenheit) {
return (fahrenheit - 32) * 5 / 9;
}

๐Ÿ’ก Idea: create a calculator that can convert between Fahrenheit, Celsius, and Kelvin.

Calculate factorial of a numberโ€‹

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

๐Ÿ’ก Idea: create a calculator for the binomial coefficient.

Calculate the average of an array of numbersโ€‹

double average(std::vector<int> arr) {
double sum = 0;
for (int i = 0; i < arr.size(); ++i) {
sum += arr[i];
}
return sum / arr.size();
}

Calculate the greatest common divisor of two numbersโ€‹

int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}

Calculate the power of a numberโ€‹

double power(double base, int exponent) {
double result = 1;
for (int i = 0; i < exponent; ++i) {
result *= base;
}
return result;
}

Reverse a stringโ€‹

std::string reverse_string(std::string str) {
int n = str.length();
for (int i = 0; i < n / 2; ++i) {
char temp = str[i];
str[i] = str[n - i - 1];
str[n - i - 1] = temp;
}
return str;
}

Check if a string is a palindromeโ€‹

bool is_palindrome(std::string str) {
int n = str.length();
for (int i = 0; i < n / 2; ++i) {
if (str[i] != str[n - i - 1]) return false;
}
return true;
}

Functions ยป Examples

Calculate area of a circleโ€‹

double area_of_circle(double radius) {
return 3.14159 * radius * radius;
}

๐Ÿ’ก Idea: Use the M_PI constant from the <cmath> header instead of 3.14159.

Convert Fahrenheit to Celsiusโ€‹

double fahrenheit_to_celsius(double fahrenheit) {
return (fahrenheit - 32) * 5 / 9;
}

๐Ÿ’ก Idea: create a calculator that can convert between Fahrenheit, Celsius, and Kelvin.

Calculate factorial of a numberโ€‹

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

๐Ÿ’ก Idea: create a calculator for the binomial coefficient.

Calculate the average of an array of numbersโ€‹

double average(std::vector<int> arr) {
double sum = 0;
for (int i = 0; i < arr.size(); ++i) {
sum += arr[i];
}
return sum / arr.size();
}

Calculate the greatest common divisor of two numbersโ€‹

int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}

Calculate the power of a numberโ€‹

double power(double base, int exponent) {
double result = 1;
for (int i = 0; i < exponent; ++i) {
result *= base;
}
return result;
}

Reverse a stringโ€‹

std::string reverse_string(std::string str) {
int n = str.length();
for (int i = 0; i < n / 2; ++i) {
char temp = str[i];
str[i] = str[n - i - 1];
str[n - i - 1] = temp;
}
return str;
}

Check if a string is a palindromeโ€‹

bool is_palindrome(std::string str) {
int n = str.length();
for (int i = 0; i < n / 2; ++i) {
if (str[i] != str[n - i - 1]) return false;
}
return true;
}