Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Arithmetic Operators Practice | Introduction to Operators
C++ Introduction
course content

Kursusindhold

C++ Introduction

C++ Introduction

1. Getting Started
2. Introduction to Operators
3. Variables and Data Types
4. Introduction to Program Flow
5. Introduction to Functions

book
Arithmetic Operators Practice

These five mathematical operators (+, -, *, /, and %) serve to carry out various mathematical operations. They work as you expect it to work and they also account for the order of the operation and paranthes. So the multiplication goes first and so on.

cpp

main

copy
1234567
#include<iostream> int main() { // Write any math expression you want std::cout << ___ << std::endl; }

The division operator (/) returns only the integer part of the result, discarding any remainder. For instance, when dividing 10 by 3, the result is 3, not 3.333... To obtain the desired division result with decimals (e.g., 10 / 3 = 3.333), it is necessary for at least one of the operands to be of a double or float data type.

cpp

main

copy
1234567
#include <iostream> int main() { std::cout << 5 / 2 << std::endl; std::cout << 5. / 2 << std::endl; }

The modulo operator (%) calculates and returns the remainder resulting from a standard division operation.

cpp

main

copy
123456
#include <iostream> int main() { std::cout << 15 % 8 << std::endl; }
Opgave

Swipe to start coding

  1. Fill in the blanks (___) with the correct arithmetic operators:
    • Use -, *, /, and % where appropriate.
    • Focus on the context of the calculations to determine the correct operator.

Løsning

cpp

solution

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 2
toggle bottom row

book
Arithmetic Operators Practice

These five mathematical operators (+, -, *, /, and %) serve to carry out various mathematical operations. They work as you expect it to work and they also account for the order of the operation and paranthes. So the multiplication goes first and so on.

cpp

main

copy
1234567
#include<iostream> int main() { // Write any math expression you want std::cout << ___ << std::endl; }

The division operator (/) returns only the integer part of the result, discarding any remainder. For instance, when dividing 10 by 3, the result is 3, not 3.333... To obtain the desired division result with decimals (e.g., 10 / 3 = 3.333), it is necessary for at least one of the operands to be of a double or float data type.

cpp

main

copy
1234567
#include <iostream> int main() { std::cout << 5 / 2 << std::endl; std::cout << 5. / 2 << std::endl; }

The modulo operator (%) calculates and returns the remainder resulting from a standard division operation.

cpp

main

copy
123456
#include <iostream> int main() { std::cout << 15 % 8 << std::endl; }
Opgave

Swipe to start coding

  1. Fill in the blanks (___) with the correct arithmetic operators:
    • Use -, *, /, and % where appropriate.
    • Focus on the context of the calculations to determine the correct operator.

Løsning

cpp

solution

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 2
Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Vi beklager, at noget gik galt. Hvad skete der?
some-alt