LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Odd math result (https://www.linuxquestions.org/questions/programming-9/odd-math-result-289443/)

Beads 02-12-2005 05:08 PM

Odd math result
 
Simple routine to calculate the area of a circle:

#include <iostream>
useing namespace std;

int main() {
float radius;
float area(3.14159 * radius * radius);'

cout << "Enter the radius ";
cin >> radius;
cout << "Area is: " << area << endl;
}


Now, a radius of 5 should yield a result of 78.xxxx (irr). But this routine yields 12.xxx.
Anyone have any ideas as to why this routine doesn't work?

:(

csfalcon 02-12-2005 05:30 PM

is this your complete code? next time please copy and paste to avoid typo.

you need to do the calculation of area after you get the radius with cin.

Code:

#include <iostream>
using namespace std;

int main() {
  float radius;
  float area;

  cout << "Enter the radius ";
  cin >> radius;
  area = 3.14159 * radius * radius;
  cout << "Area is: " << area << endl;
}


Beads 02-12-2005 07:42 PM

You are quite correct, csfalcon. That worked. :rolleyes:
Thank you very much! :)


All times are GMT -5. The time now is 05:07 PM.