188k views
12 votes
I want to write a C++ program to read the diameter of a circle. The program should find and display the area and circumference of the circle. Define a constant of type double PI = 3.14159

I need code for this program

User Serjux
by
8.8k points

1 Answer

9 votes

Answer:

#include <iostream>

const double PI = 3.14159;

int main() {

double diameter;

std::cout << "Please enter the diameter of the circle:" << std::endl;

std::cin >> diameter;

double radius = diameter / 2;

double area = radius * radius * PI;

double circumference = 2 * radius * PI; // or diameter * PI;

std::cout << "Area: " << area << std::endl;

std::cout << "Circumference: " << circumference << std::endl;

}

Step-by-step explanation:

The first line imports the read/write library. The rest should be self-explanatory.

std::cin is the input stream - we use it to get the diameter

std::cout is the output stream - we write the answers (and commentary) there.

std::endl is the special "character" informing the output stream that we want it to put a newline character (and actually print to the output - it might have been holding off on it).

the // in the circumference computation signifies a comment - the program simply ignores this. I've added it to tell the reader that the circumference could have been computed using the diameter,

User Angella
by
8.2k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.