How to calculate Percentage in C++

Now, let's learn how to use our basic C++ knowledge to make really useful programs. First, let's look at how to calculate the most diverse types of percentages.

Percentage in C++


  • "Make a program that calculates 12% of 2112"
The name gives us a clue: percentage ... per cent ... per hundred
That is, we must divide by 100.

12 divided by 100 is 0.12, so 12% of 2100 is:
0.12 * 2112

#include <iostream>
using namespace std;

int main()
{
    cout << "12% of 2112= " << 0.12*2112;
    return 0;
}


  • "Make a program that receives a user value and calculates 12% of that total."

#include <iostream>
using namespace std;

int main()
{
    float num;
    cout << "Type a value: ";
    cin >> num;
    cout << "12% of "<< num <<" = "
         << 0.12 * num;
    return 0;
}

  • "Make a program that gets a percentage value from the user, and calculates how much that represents from a second value that he typed. For example, if he types 4 and 400, his program should calculate how much is 4% of 400"

#include <iostream>
using namespace std;

int main()
{
    float percentage, num;
    cout << "Percentage value: ";
    cin >> percentage;
    percentage = percentage/100;

    cout << "Second value: ";
    cin >> num;

    cout << percentage <<"% of "
         << num << " = "<< percentage*num;
    return 0;
}

Note the user will enter a value such as 12 to calculate 12%.
But in the calculation, we do not use 12 but 0.12 (this value by percent, that is, this value divided by 100).

So, we see this news:
percentage = percentage / 100;

A priori, it sounds strange, but we are just dividing the percentage variable by 100.
This means: the new value of percentage is equal to the old value divided by 100.

You could have done this by declaring another aux variable:
aux = percentage / 100;
percentage = aux;

But then I'd allocate memory for nothing, better do what we did in the code, it's simpler, faster and more efficient (yes, programmers are lazy, always want to write as little code as possible).

Percentages in C++


  • "Program one software that receives two numbers, where the first must be less than the second. It then calculates the percentage that the first represents from the second. For example, if you entered 12 and 21, that means 12 represents 57.14% of 21 "

#include <iostream>
using namespace std;

int main()
{
    float first, second;
    cout << "First number: ";
    cin >> first;

    cout << "Second number: ";
    cin >> second;

    cout << first <<"% of "
         << second << " = "<< (first/second)*100.0;
    return 0;
}

Here, the only thing different is to remember to multiply by 100 to display the result as a percentage.

How to calculate percentage in C++


  • "You have become a C ++ programmer, and now you are earning super good. But you will still have to pay taxes. Create software that receives your salary and calculates the 7% income tax. The output of your program should be gross salary (no rebate), the amount of tax you will pay and your net salary (after deducting taxes). "

#include <iostream>
using namespace std;

int main()
{
    float salary;
    cout << "Type your salary: ";
    cin >> salary;

    cout << "Gross salary: $ " << salary << endl;
    cout << "7% from taxes: $ " << 0.07*salary << endl;
    cout << "Net salary: $ " << 0.93*salary << endl;

    return 0;
}

If it is discounted 7%, it means that you get 93% of gross salary, which is the net.

How to make percentage in C++


  • "Due to inflation, every year your salary must be adjusted based on it.
    For example, if inflation was 2.5% then your salary should grow by the same amount so as not to lose value.
    Create a C ++ program that asks for a person's salary, last year's inflation, and apply that inflation. Show the previous salary, the increase due to inflation and the new salary. "

The salary increase corresponds to the previous value multiplied by 0.025 (2.5%).
The new salary is the previous one plus this 2.5%: 1 + 0.025 = 1.025 * (previous salary).

Look:
#include <iostream>
using namespace std;

int main()
{
    float salary;
    cout << "What's your salary: ";
    cin >> salary;

    cout << "Previous salario: $ " << salary << endl;
    cout << "2.5% increase: $ " << 0.025*salary << endl;
    cout << "New salary: $ " << 1.025*salary << endl;

    return 0;
}

And take percentage

"In the city of C++land there is a tolerance of 15% of the speed limit,to not to get a fine. Make a program that asks the user for the maximum speed of a road and calculates how fast the car can travel without being fined. Your code will be embedded in the car's GPS system to warn the speed limit the car must travel."

If the maximum speed of the road is speed, then the maximum speed with tolerance not to be fined is: 100% + 15% = 115% = 1.15 * speed

#include <iostream>
using namespace std;

int main()
{
    float speed;
    cout << "Maximum speed: ";
    cin >> speed;

    cout << "Maximum speed: " << speed << endl;
    cout << "15% tolerance: " << 0.15*speed << endl;
    cout << "Maximum speed without penalty: " << 1.15*speed << endl;

    return 0;
}

No comments:

Post a Comment