Exercise 2-2 (Circle)

Chapter_2     Exercise_2-1     Numconv CallHello     Exercise_2-3







Exercise 2-2     TCP1, p. 120


Exercise 2-2. Using Stream.cpp and Numconv.cpp as guidelines, create a program that asks for the radius of a circle and prints the area of that circle. You can just use the '*' operator to square the radius. Do not try to print out the value as octal or hex (these only work with integral types).




Circle.cpp         download


// prints the area of a circle, knowing the radius
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{
float radius;

cout << "Enter the radius: ";
cin >> radius;
cout << "Area: " << 3.14 * radius * radius << endl;
}
/*
g++ Circle.cpp -o Circle
./Circle
Enter the radius: 2.7
Area: 22.8906
*/









Chapter_2     Exercise_2-1     Numconv BACK_TO_TOP CallHello     Exercise_2-3



Comments

Popular posts from this blog

Contents