Write a C++ Program to Find Area of Circle using Const

C++ Program to Find Area of Circle

In geometry, the area enclosed by a circle of radius r is π r². π is the Greek letter which represents a constant, approximately equal to 3.14159, which equals the ratio of the circumference of any circle to its diameter;

In C++ Program you will have to apply the formula, A= π*r*r or A= π r²

Here,

A= area
r= radius;
π= 3.14159 approx;

Now let’s write a program to find an area of the Circle using Const;

Note: You might get confused here because we’ve used Const variable to represent the PI;

Learn: How to Write “Hello, World! Program in C++

What is Const or Constant in C++?

Const is the keyword which is known as Constant, if present, precedes the data type of a variable. It specifies that the value of the variable will not change throughout the program. Any attempt to alter the value of the variable defined with this qualifier will result into an error message from the compiler. Const is usually used to replace defined constant. Variables with this qualifier are often named in all uppercase, as a reminder that they are constants. The following program shows the usage of const.

C++ Program to Find Area of Circle using Const

#include<iostream>
using namespace std;

int main(){

float r,a;
const float PI=3.14;

cout<<"Enter R:"<<endl;
cin>>r;
a= PI*r*r;
cout<<endl<<"Area of Circle="<<a;

}

Output

Enter R:
10

Area of Circle=314
RUN SUCCESSFUL (total time: 10s)

That’s how it works with the constant, variable;

Leave a Reply

Your email address will not be published. Required fields are marked *