How to Display Percentages in C++

A percentage is a ratio expressed as part of one hundred. It is written as a number followed by the percent sign (%). C++ classifies numbers with several different data types. One of them is an integer. Another is a float, which is used for decimal assignments and calculations. A double is a type that can also handle integer assignments, but it is more precise than a float. To display percentages in C++, cast the variables as floats or doubles. After the calculations are done, treat the percent sign as a string by enclosing it with quotations.

Video of the Day

Use quotation marks to help display percentages in C++.


Write a C++ file with the iostream header, namespace and int main().

includeusing namespace std;

int main() {


Create float variables. Given variables x, y and answer, the code is:

float x; float y; float answer;


Instruct the user to input two numbers. As an example, write:

cout<<"Enter the first number : "; cin >> x; cout << "Enter the second number: "; cin >> y;


Perform a percentage calculation and assign the result to answer. Write:


Output the variables. After answer, add the percent sign and enclose it in quotations so that the compiler will treat it as a string. For instance, write:

cout << x << " over " << y << " is " << answer << "%" << endl;


End the program with return 0 and a bracket. Compile and run it. A test program where x is 5 and y is 7 yields answer = 71.4826%.

Show Comments