Exercises

Table of contents

Roman numerals


int w[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
char *s[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};

int n, r;
cout << "Enter an integer: ";
cin >> n;
if (n <= 0)
    return 0;
    
for (r = 0; n > 0;) {
    if (n >= w[r]) {
        cout << s[r];
        n -= w[r];
    }
    else
        r++;
}
                                    

Reversing a string


string word = "abcdefgh";
for (int i = word.length() - 1; i >= 0; i--) // "length - 1" so that we don't see a space at the beginning (in was at the end)
    cout << word[i];
                                    

Checking whether a word is a palindrome

A palindrome is a word, sentence, verse, or even number that reads the same backward or forward.


bool palindrome = true;
string word = "abba";

for (int i = 0; i < word.length() / 2; i++) {
    if (word[i] != word[word.length() - 1 - i]) {
        palindrome = false;
        break;
    }
}

if (palindrome == true)
    cout << word << " is a palindrome." << endl;
else
    cout << word << " is not a palindrome." << endl;
                                    

Searching for an element inside an array


int a[5] = {4, -2, 8, 40, -20};
int x = -1, y;

cout << "Enter the number you are searching for: ";
cin >> y;

for (int i = 0; i < 5; i++) {
    if (y == a[i])
        x = i;
}
if (x != -1)
    cout << "Your number was found under this index: " << x << endl;
else
    cout << "This element doesn't exist." << endl;