basic math coding cheat sheet
1. Count the number of digits in n:
#include <iostream>
using namespace std;
int countDigits(int n) {
int count = 0;
while (n != 0) {
n /= 10;
++count;
}
return count;
}
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
cout << "Number of digits: " << countDigits(n) <<
endl;
return 0;
}
2. Reverse a number:
#include <iostream>
using namespace std;
int reverseNumber(int n) {
int reversed = 0;
while (n != 0) {
int digit = n % 10;
reversed = reversed * 10 + digit;
n /= 10;
}
return reversed;
}
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
cout << "Reversed number: " << reverseNumber(n) <<
endl;
return 0;
}
3. Check if a number is a palindrome:
#include <iostream>
using namespace std;
bool isPalindrome(int n) {
int original = n;
int reversed = 0;
while (n != 0) {
int digit = n % 10;
reversed = reversed * 10 + digit;
n /= 10;
}
return original == reversed;
}
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
if (isPalindrome(n))
cout << n << " is a palindrome." << endl;
else
cout << n << " is not a palindrome." << endl;
return 0;
}
4. GCD or HCF of two numbers:
#include <iostream>
using namespace std;
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "GCD (HCF): " << gcd(a, b) << endl;
return 0;
}
5. Check if a number is an Armstrong number:
#include <iostream>
#include <cmath>
using namespace std;
bool isArmstrong(int n) {
int original = n;
int sum = 0;
int digits = log10(n) + 1;
while (n != 0) {
int digit = n % 10;
sum += pow(digit, digits);
n /= 10;
}
return original == sum;
}
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
if (isArmstrong(n))
cout << n << " is an Armstrong number." <<
endl;
else
cout << n << " is not an Armstrong number." <<
endl;
return 0;
}
6. Print all divisors of a number:
#include <iostream>
using namespace std;
void printDivisors(int n) {
cout << "Divisors of " << n << " are: ";
for (int i = 1; i <= n; ++i) {
if (n % i == 0)
cout << i << " ";
}
cout << endl;
}
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
printDivisors(n);
return 0;
}
7. Check if a number is prime:
#include <iostream>
using namespace std;
bool isPrime(int n) {
if (n <= 1)
return false;
for (int i = 2; i <= sqrt(n); ++i) {
if (n % i == 0)
return false;
}
return true;
}
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
if (isPrime(n))
cout << n << " is a prime number." << endl;
else
cout << n << " is not a prime number." << endl;
return 0;
}
Recursion
1. Understand recursion by printing something N times:
#include <iostream>
using namespace std;
void printNTimes(int n) {
if (n <= 0) return;
cout << "Recursion Example" << endl;
printNTimes(n - 1);
}
int main() {
int n;
cout << "Enter the number of times to print: ";
cin >> n;
printNTimes(n);
return 0;
}
2. Print name N times using recursion:
#include <iostream>
using namespace std;
void printNameNTimes(int n) {
if (n <= 0) return;
cout << "A" << endl;
printNameNTimes(n - 1);
}
int main() {
int n;
cout << "Enter the number of times to print the
name: ";
cin >> n;
printNameNTimes(n);
return 0;
}
3. Print 1 to N using recursion:
#include <iostream>
using namespace std;
void print1ToN(int n) {
if (n <= 0) return;
print1ToN(n - 1);
cout << n << " ";
}
int main() {
int n;
cout << "Enter the value of N: ";
cin >> n;
print1ToN(n);
return 0;
}
4. Print N to 1 using recursion:
#include <iostream>
using namespace std;
void printNTo1(int n) {
if (n <= 0) return;
cout << n << " ";
printNTo1(n - 1);
}
int main() {
int n;
cout << "Enter the value of N: ";
cin >> n;
printNTo1(n);
return 0;
}
5. Sum of first N numbers:
#include <iostream>
using namespace std;
int sumOfN(int n) {
if (n <= 0) return 0;
return n + sumOfN(n - 1);
}
int main() {
int n;
cout << "Enter the value of N: ";
cin >> n;
cout << "Sum of first " << n << " numbers: " <<
sumOfN(n) << endl;
return 0;
}
6. Factorial of N numbers:
#include <iostream>
using namespace std;
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
int main() {
int n;
cout << "Enter the value of N: ";
cin >> n;
cout << "Factorial of " << n << " is " <<
factorial(n) << endl;
return 0;
}
7. Reverse an array:
#include <iostream>
using namespace std;
void reverseArray(int arr[], int start, int end) {
if (start >= end) return;
swap(arr[start], arr[end]);
reverseArray(arr, start + 1, end - 1);
}
int main() {
int n;
cout << "Enter the size of the array: ";
cin >> n;
int arr[n];
cout << "Enter the elements of the array: ";
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
reverseArray(arr, 0, n - 1);
cout << "Reversed array: ";
for (int i = 0; i < n; ++i) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
8. Check if a string is a palindrome or not:
#include <iostream>
using namespace std;
bool isPalindrome(string str, int start, int end) {
if (start >= end) return true;
if (str[start] != str[end]) return false;
return isPalindrome(str, start + 1, end - 1);
}
int main() {
string str;
cout << "Enter a string: ";
cin >> str;
if (isPalindrome(str, 0, str.size() - 1))
cout << str << " is a palindrome." << endl;
else
cout << str << " is not a palindrome." << endl;
return 0;
}
9. Fibonacci Number:
#include <iostream>
using namespace std;
int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n;
cout << "Enter the position of the Fibonacci
number: ";
cin >> n;
cout << "Fibonacci number at position " << n << "
is " << fibonacci(n) << endl;
return 0;
}