Write a program to calculate area of circle in c++


Asked by admin @ in Computers and Technology viewed by 311 People


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

Answered by admin @



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;

}

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,


Similar Questions

C++ program to calculate area of rectangle using constructor

Asked by admin @ in Computer Science viewed by 346 persons

C++ program to calculate area of traingle circle and rectangle using constructor overloading

Write a c++ program to calculate gpa in a semester

Asked by admin @ in Computers and Technology viewed by 351 persons

Write a C++ program that opens and reads file question2.txt. File question1.txt has 3 students and 3 grades of each. You program should read values from file and assign values …

How to calculate the area and perimeter of a circle

Asked by admin @ in Physics viewed by 283 persons

Write an algorithm and draw the flowchart of finding the area and perimeter of a circle. The radius (float) need to be entered by the user and if the user …

What is the sum of the areas of circle c and circle d? 7 14 49 98

Asked by maham237 @ in Mathematics viewed by 480 persons

Which of the following could be used to calculate the area of the sector in the circle shown above?

Asked by maham237 @ in Mathematics viewed by 339 persons

Circle A is shown with a central angle marked 40 degrees and the radius marked 15 inches.

In circle c, r = 32 units. what is the area of circle c? 32 64 256 1024

Asked by maham237 @ in Mathematics viewed by 499 persons

Write a c++ program to accept and print your name

Asked by admin @ in Computer Science viewed by 325 persons

Write a C++ program to accept and print your name?​

Write a program to calculate compound interest in python

Asked by admin @ in Physics viewed by 415 persons

Write a program in python to calculate the compound interest​

Write the equation of the circle in standard form calculator

Asked by admin @ in Mathematics viewed by 323 persons

Determine the equation of a circle with a center at (–4, 0) that passes through the point (–2, 1) by following the steps below. Use the distance formula to determine …

C programing

Asked by pranav94 @ in Engineering viewed by 469 persons

How to find area of a circle when given circumference

Asked by admin @ in Mathematics viewed by 300 persons

Explain each step in the process of finding the area of a circle given the circumference

How are the circumference and area of a circle related

Asked by admin @ in Mathematics viewed by 262 persons

How are arc length and sector area related to circumference and area of a circle

How to calculate the surface area of a hexagonal prism

Asked by admin @ in Mathematics viewed by 371 persons

The figure shows a regular hexagonal prism. The figure shows a regular hexagonal prism. The side of the hexagonal face is labeled as 4 centimeters, the length of the rectangular …

How to find the lateral area of a prism calculator

Asked by admin @ in Mathematics viewed by 393 persons

1. use formulas to find the lateral area and surface area of the given prism. Round your answer to the nearest whole number. Use the large 12 * 7 m …

How to find area of a sector of a circle

Asked by admin @ in Mathematics viewed by 365 persons

The area A of a sector of a circle is given by A=πr²s/360, where r is the radius of the circle and s is the angle measure(in degrees) of the …

Most viewed questions in Computers And Technology


A customer wants to increase the storage capacity by 25gb

Asked by admin @ in Computers and Technology viewed by 4430 persons




Solid yellow line bordering your lane of traffic marks a

Asked by admin @ in Computers and Technology viewed by 606 persons



Which of the following is not a windows utility program

Asked by admin @ in Computers and Technology viewed by 591 persons


When is a wrecker considered to be an emergency vehicle

Asked by admin @ in Computers and Technology viewed by 573 persons


Which operation on a pwc requires more than idle speed

Asked by admin @ in Computers and Technology viewed by 573 persons



Which of the following statements is true of a database

Asked by admin @ in Computers and Technology viewed by 559 persons


Which of the following is not true about an entrepreneur

Asked by admin @ in Computers and Technology viewed by 535 persons


Kitchen gadgets draw a context diagram for the order system

Asked by admin @ in Computers and Technology viewed by 512 persons