Working with Numbers in C++
C++ supports various numeric data types for handling numbers, including integers and floating-point numbers.
Key Topics
Integers
Integers are whole numbers without a fractional part. The int
data type is used to declare integer variables.
Example: Integer Variables
int age = 30;
int year = 2023;
Explanation: Variables age
and year
are declared as integers and assigned whole number values.
Key Takeaways
- Use
int
for whole numbers. - Be aware of the range of values an
int
can hold.
Floating-Point Numbers
Floating-point numbers are numbers with a fractional part. Use float
or double
to declare such variables.
Example: Floating-Point Variables
float temperature = 36.6f;
double pi = 3.1415926535;
Explanation: Variable temperature
is a float
, and pi
is a double
, representing numbers with decimal points.
Key Takeaways
- Use
float
ordouble
for decimal numbers. double
offers more precision thanfloat
.