Floating point control in a for loop : for loop « Operators statements « C++ Tutorial

Home
C++ Tutorial
1.Language Basics
2.Data Types
3.Operators statements
4.Array
5.Development
6.Exceptions
7.Function
8.Structure
9.Class
10.Operator Overloading
11.Pointer
12.File Stream
13.template
14.STL Introduction
15.string
16.vector
17.list
18.bitset
19.set multiset
20.valarray
21.queue stack
22.deque
23.map multimap
24.STL Algorithms Modifying sequence operations
25.STL Algorithms Non modifying sequence operations
26.STL Algorithms Binary search
27.STL Algorithms Sorting
28.STL Algorithms Merge
29.STL Algorithms Min Max
30.STL Algorithms Iterator
31.STL Algorithms Heap
32.STL Algorithms Helper
C / ANSI-C
C Tutorial
C++
Visual C++ .NET
C++ Tutorial » Operators statements » for loop 
3.15.14.Floating point control in a for loop
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;

int main() {
  const double pi = 3.14;
  cout << endl;

  for(double radius = .2 ; radius <= 3.0 ; radius += .2)
    cout << "radius = " << std::setw(6<< radius
         << "  area = " << std::setw(12<< pi * radius * radius
         << endl;
  return 0;
}
radius =    0.2  area =       0.1256
radius =    0.4  area =       0.5024
radius =    0.6  area =       1.1304
radius =    0.8  area =       2.0096
radius =      1  area =         3.14
radius =    1.2  area =       4.5216
radius =    1.4  area =       6.1544
radius =    1.6  area =       8.0384
radius =    1.8  area =      10.1736
radius =      2  area =        12.56
radius =    2.2  area =      15.1976
radius =    2.4  area =      18.0864
radius =    2.6  area =      21.2264
radius =    2.8  area =      24.6176
3.15.for loop
3.15.1.Simplest for loop statement
3.15.2.A conversion table of feet to meters
3.15.3.Use multiple statements in for loops
3.15.4.Display the alphabet: use char to control for loop
3.15.5.A negatively running for loop
3.15.6.Loop until a random number that is greater than 20,000.
3.15.7.A for loop with no increment
3.15.8.The body of a for loop can be empty
3.15.9.Declare loop control variable inside the for
3.15.10.Use nested for loops to find factors of numbers between 2 and 100
3.15.11.For loops with null statements
3.15.12.empty for loop statement
3.15.13.Compound interest calculations with for
3.15.14.Floating point control in a for loop
3.15.15.Generating multiplication tables
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.