I am making a circumference finder program by multiplying the diameter of circle with PI. But I want to take the input of diameter from the user. Can you figure out how to do that? Also, I want the answer as a float or double (I mean with decimalS) but answer comes in integer (without decimals) form.
Code -
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
usingnamespace std;
int main()
{
constdouble PI = 22/7;
cout<<"Circle Circumference Finder!"<< endl;
cout<<"Plz enter the diameter of the circle"<< endl; // Consider the input value as x
cout<<"The circumference of the circle is: "<<(PI * x) << endl;
return 0;
}
On line 7, you are doing an integer division, which rounds out the remainder before PI recieves it. Its better normally to just define PI in the normal way (i.e., constdouble PI = 3.1415926535897932 or something like that). Also, to input, just make a doublex and use std::cin to input into the variable, and then you can multiply them together.