Skip to content
Laviyx Learn

Try

C++ Basics Practice

Practise C++ variables, data types, arithmetic operators, integer division, type conversion and formatted output.

Beginner7 problemsUpdated

Review first: Variables, Data Types, Operators, Type Conversion, Constants

  1. 1.Split the bill

    Beginner

    A dinner costs 1850 rupees and 4 friends split it equally. Print each share with exactly two decimal places, like Each person pays 462.50.

    Show hint

    Make sure the division is done in floating point, then use std::fixed and std::setprecision(2) from <iomanip>.

    Show solution
    C++
    #include <iomanip>
    #include <iostream>
    
    int main() {
        double total = 1850;
        int people = 4;
    
        double share = total / people;
        std::cout << std::fixed << std::setprecision(2);
        std::cout << "Each person pays " << share << '\n';
    }
    Output
    Each person pays 462.50
  2. 2.Minutes to hours and minutes

    Beginner

    A film lasts 135 minutes. Print it as 2 h 15 min.

    Show hint

    Integer / gives the whole hours and % gives the minutes left over.

    Show solution
    C++
    #include <iostream>
    
    int main() {
        int minutes = 135;
        std::cout << minutes / 60 << " h " << minutes % 60 << " min\n";
    }
    Output
    2 h 15 min
  3. 3.Temperature conversion

    Beginner

    Convert a body temperature of 36.6 °C to Fahrenheit (F = C × 9 / 5 + 32) and print it with one decimal place.

    Show hint

    Use double for the temperature, and std::fixed with std::setprecision(1).

    Show solution
    C++
    #include <iomanip>
    #include <iostream>
    
    int main() {
        double celsius = 36.6;
        double fahrenheit = celsius * 9 / 5 + 32;
        std::cout << std::fixed << std::setprecision(1) << fahrenheit << " F\n";
    }
    Output
    97.9 F

    The exact value is 97.88, which rounds to 97.9 with one decimal place.

  4. 4.Fix the average

    Beginner

    The average of the marks 7, 8 and 10 should be about 8.33, but int sum = 25; double avg = sum / 3; gives 8. Print the wrong result and then the correct one.

    Show hint

    sum / 3 divides two integers before the result is stored. Cast one operand to double first.

    Show solution
    C++
    #include <iostream>
    
    int main() {
        int sum = 7 + 8 + 10;
    
        double wrong = sum / 3;
        double right = static_cast<double>(sum) / 3;
    
        std::cout << wrong << '\n' << right << '\n';
    }
    Output
    8
    8.33333

    std::cout prints at most 6 significant digits by default, so 8.333333… appears as 8.33333.

  5. 5.First and last digit

    Beginner

    For the 4-digit number 4096, print its first digit, its last digit, and their sum.

    Show hint

    The last digit is n % 10. For a 4-digit number, the first digit is n / 1000.

    Show solution
    C++
    #include <iostream>
    
    int main() {
        int n = 4096;
        int first = n / 1000;
        int last = n % 10;
        std::cout << first << ' ' << last << ' ' << first + last << '\n';
    }
    Output
    4 6 10
  6. 6.Avoid integer overflow

    Beginner

    A square stadium floor is 50000 mm on each side. Print its area in square millimetres. The answer is larger than an int can hold.

    Show hint

    50000 * 50000 overflows a 32-bit int. Do the multiplication in long long.

    Show solution
    C++
    #include <iostream>
    
    int main() {
        long long side = 50000;
        long long area = side * side;
        std::cout << area << '\n';
    }
    Output
    2500000000

    An int holds up to 2,147,483,647, and 2,500,000,000 is bigger. Because side is a long long, the multiplication itself is done in 64 bits. Writing int side = 50000; long long area = side * side; would still overflow, because the multiplication happens in int before the result is stored.

  7. 7.Characters are numbers

    Beginner

    Given char letter = 'C';, print the next letter of the alphabet and the numeric code of letter.

    Show hint

    A char is a small integer, so letter + 1 is the next code. Cast to char to print it as a letter, and to int to print the code.

    Show solution
    C++
    #include <iostream>
    
    int main() {
        char letter = 'C';
        char next = static_cast<char>(letter + 1);
        std::cout << next << ' ' << static_cast<int>(letter) << '\n';
    }
    Output
    D 67