C++ How To's
C++ How To's
These examples show how to solve common tasks in C++.
Numbers
- Add Two Numbers - Add two numbers and print the result.
- Swap Two Variables - Exchange the values of two variables.
- Even or Odd Number - Check if a number is even or odd.
- Reverse a Number - Reverse the digits of a number.
- Positive or Negative - Check if a number is positive, negative or zero.
- Square Root - Find the square root of a number.
- Area of Rectangle - Calculate the area of a rectangle.
- Celsius to Fahrenheit - Convert a temperature from Celsius to Fahrenheit.
- Sum of Digits - Add up the digits of a number.
- Armstrong Number - Check if a number is an Armstrong number.
- Random Number - Generate a random number.
Strings
- Count Words - Count how many words are in a sentence.
- Count Vowels in a String - Count how many vowels are in a string.
- Remove Vowels - Remove the vowels from a string.
- Count Digits in a String - Count the digits in a string.
- Reverse a String - Reverse the characters in a string.
- Palindrome Check - Check if a string is the same forward and backward.
- Check Anagram - Check if two strings have the same characters in a different order.
- Split a String - Split a string into words, or at a separator like a comma.
- Remove Whitespace - Remove spaces, tabs and newlines from a string.
- Count Character Frequency - Count how many times each character appears in a string.
Arrays
- Sum of Array Elements - Add all the elements in an array.
- Find Array Average - Calculate the average value of array elements.
- Sort an Array - Sort the elements of an array.
- Find Smallest Element - Find the lowest value in an array.
- Find Largest Element - Find the largest value in an array.
- Second Largest Array - Find the second largest value in an array.
- Min and Max in Array - Find the minimum and maximum value in an array.
- Merge Two Arrays - Combine two arrays into one.
- Remove Duplicates - Remove duplicate values from an array by using a set.
- Find Duplicates - Find the elements that appear more than once in an array.
- Shuffle an Array - Put the elements of an array in a random order.
Algorithms
- Factorial of a Number - Calculate the factorial of a number.
- Fibonacci Sequence - Print the first numbers of the Fibonacci sequence.
- Find GCD - Find the greatest common divisor of two numbers.
- Check Prime Number - Check if a number is a prime number.
Loops
- Loop Through a Vector - Loop through all the elements of a vector.
- Loop Through a Map - Loop through all the keys and values of a map.