Const Variable in C++

Level 265 keystrokes

Declares the constant PI with const and uses it to compute the area of a circle with radius 5; cout prints 78.5397.

C++ code

// const makes PI read-only: assigning to it later is a compile error
1const double PI = 3.14159;
// Stores the radius 5 in r
2int r = 5;
// Area = PI * r * r = 3.14159 * 25 = 78.53975
// The double is actually 78.5397499…, just under 78.53975, so cout's default 6 significant digits print 78.5397
3cout << PI * r * r << endl;

Output

78.5397
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C++ code and how often they appear.

  • <× 4
  • ;× 3
  • *× 2
  • =× 2
  • .× 1

More C++ examples at level 2

← C++ typing practice