c++ how to make a negative float positive
To make a negative float positive in C++, you can use the unary plus operator. The unary plus operator (+) can be used to explicitly convert a negative value to a positive value. Here are the steps to make a negative float positive:
- Declare a float variable and assign a negative value to it:
cpp
float negativeFloat = -10.5;
In this example, the variable negativeFloat
is assigned a negative value of -10.5.
- Use the unary plus operator to convert the negative value to a positive value:
cpp
float positiveFloat = +negativeFloat;
In this step, the unary plus operator (+) is used to convert the negative value stored in negativeFloat
to a positive value. The positive value is then assigned to the variable positiveFloat
.
- Print the positive float value:
cpp
std::cout << "Positive float: " << positiveFloat << std::endl;
This step prints the positive float value to the console.
Here's the complete code example:
#include <iostream>
int main() {
float negativeFloat = -10.5;
float positiveFloat = +negativeFloat;
std::cout << "Positive float: " << positiveFloat << std::endl;
return 0;
}
When you run this code, it will output:
Positive float: 10.5
This demonstrates how to use the unary plus operator to make a negative float positive in C++.