C++ Examples: Basic Operations and Code Snippets

C++ Code Examples

Basic Output

#include <iostream>
using namespace std;

int main(){
    cout << "Hola a todos!" << endl;
}

Sum of Two Integers

#include <iostream>
using namespace std;

int main() {
    int x, y;
    cin >> x >> y;
    cout << x + y << endl;
}

Sum of Three Integers

#include <iostream>
using namespace std;

int main() {
    int x, y, z;
    cin >> x >> y >> z;
    cout << x + y + z << endl;
}

Maximum of Two Integers

#include <iostream>
using namespace std;

int main() {
    int x, y;
    cin >> x >> y;
    if (x > y) {
        cout << x << endl;
    } else {
        cout << y << endl;
    }
}

Maximum of Three Integers

#include <iostream>
using namespace std;

int main()
{
    int max;
    int num1 = 0, num2 = 0, num3 = 0;
    cin >> num1;
    cin >> num2;
    cin >> num3;
    if (num1 > num2) {
        max = num1;
    } else {
        max = num2;
    }
    if (num3 > max) {
        max = num3;
    }
    cout << max << endl;
}

Maximum of Three Different Numbers

#include <iostream>
using namespace std;

int main()
{
    int max;
    int num1 = 0, num2 = 0, num3 = 0;
    cin >> num1;
    cin >> num2;
    cin >> num3;
    if (num1 > num2) {
        max = num1;
    } else {
        max = num2;
    }
    if (num3 > max) {
        max = num3;
    }
    cout << max << endl;
}

Integer Division

#include <iostream>
using namespace std;

int main() {
    int x, y;
    int quotient, remainder;
    cin >> x >> y;
    quotient = x / y;
    remainder = x % y;
    cout << quotient << " " << remainder << endl;
}

Uppercase and Lowercase

#include <iostream>
using namespace std;

int main() {
    char x;
    cin >> x;
    if (x >= 97) {
        cout << char(int(x) - 32) << endl;
    } else {
        cout << char(int(x) + 32) << endl;
    }
}

Character Classification

#include <iostream>
using namespace std;

int main() {
    char a;
    cin >> a;
    if (a < 96) {
        cout << "uppercase" << endl;
    } else {
        cout << "lowercase" << endl;
    }
    if ((a == 65) or (a == 69) or (a == 73) or (a == 79) or (a == 85) or (a == 97) or (a == 101) or (a == 105) or (a == 111) or (a == 117)) {
        cout << "vowel" << endl;
    } else {
        cout << "consonant" << endl;
    }
}

String Comparison

#include <iostream>
#include <string>

using namespace std;

int main() {
    string p, q;
    cin >> p >> q;
    if (p > q) {
        cout << p << " > " << q << endl;
    } else if (p == q) {
        cout << p << " = " << q << endl;
    } else {
        cout << p << " < " << q << endl;
    }
}

Rounding

#include <iostream>

using namespace std;

int main() {
    double n;
    cin >> n;
    if (int(n) == n) {
        cout << int(n) << " " << int(n) << " " << int(n) << endl;
    } else if (n - int(n) >= 0.5) {
        cout << int(n) << " " << int(n) + 1 << " " << int(n) + 1 << endl;
    } else {
        cout << int(n) << " " << int(n) + 1 << " " << int(n) << endl;
    }
}

Time Decomposition

#include <iostream>
using namespace std;

int main() {
    int x;
    cin >> x;
    cout << x / 3600 << " " << x % 3600 / 60 << " " << x % 60 * 1 << endl;
}

Add One Second

#include <iostream>
using namespace std;

int main() {
    int h, m, s;
    cin >> h >> m >> s;
    s = s + 1;
    if (s >= 60) {
        s = 0;
        m = m + 1;
        if (m >= 60) {
            m = 0;
            h = h + 1;
            if (h >= 24) {
                h = 0;
            }
        }
    }
    if (h < 10) {
        cout << 0 << h << ":";
    } else {
        cout << h << ":";
    }
    if (m < 10) {
        cout << 0 << m << ":";
    } else {
        cout << m << ":";
    }
    if (s < 10) {
        cout << 0 << s << endl;
    } else {
        cout << s << endl;
    }
}

Leap Years

int main() {
    int n;
    cin >> n;
    if (n % 100 == 0) {
        if (n % 400 == 0) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    } else {
        if (n % 4 == 0) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }
}