![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Man command doesn't display any output amonst other problems | djdavies | AIX | 6 | 02-02-2007 06:07 AM |
| Man command doesn't display any output amonst other problems :-) | djdavies | UNIX for Dummies Questions & Answers | 14 | 02-01-2007 11:42 AM |
| How do I stop printf output from going into seperate txt files | chrchcol | Shell Programming and Scripting | 12 | 07-26-2006 10:08 PM |
| find: problems escaping printf-command string | grahamb | Shell Programming and Scripting | 1 | 12-04-2005 04:00 PM |
| fixed-width printf() output on an XmList on Solaris | trido | High Level Programming | 0 | 11-20-2002 05:21 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Code:
#include <stdio.h>
#include <math.h>
// this function calculates the volume of a Cylinder
int main(void)
{
int r; // radius
int h; // height
double M_PI; // pi
int pOne = pow (r, 2);
// get user input of radius and height
printf ("Enter your radius: ");
scanf ("%f", &r);
printf ("Enter your height: ");
scanf ("%f", &h);
// calculate volume
volumeCylinder = pOne * M_PI * h;
// output volume result
printf ("The Volume of the Cylinder is: %lf\n", volumeCylinder);
return 0;
}
Sample Output: Code:
~Enter your radius: 3 ~Enter your height: 2 ~The Volume of the Cylinder is: 0.000000 ~ The program compiles and runs but no matter what kind of printf format I do (double, float, int) I always get 0.0000..., -0.000..., or some really large number. I don't know what could be wrong. Can anybody please help me with this? It is driving me insane. |
|
||||
|
1. You have calculate the power of r before assigning a value to it 2. The scanf function isn't used properly 3. M_PI is already defined in math.h 4. You haven't declare volumeCylinder before using it Try this: Code:
#include <stdio.h>
#include <math.h>
// this function calculates the volume of a Cylinder
int main(void)
{
int r; // radius
int h; // height
double volumeCylinder;
// get user input of radius and height
printf ("Enter your radius: ");
scanf ("%d", &r);
printf ("Enter your height: ");
scanf ("%d", &h);
// calculate volume
int pOne = pow (r, 2);
volumeCylinder = pOne * M_PI * h;
// output volume result
printf ("The Volume of the Cylinder is: %lf\n", volumeCylinder);
return 0;
}
Regards |
![]() |
| Bookmarks |
| Tags |
| calculate, math.h, printf |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|