Write an interactive C or C++ program to calculate the surface area of a cylinder. In the header comment of your code, write the pseudocode for the program. Include a screenshot showing that's you have tested your program.
Write an interactive C or C++ program to calculate the surface area of a cylinder. In the header comment of your code, write the pseudocode for the program. Include a screenshot showing that's you have tested your program.
cylinder surface area calculates with formula: 2*pi*R*h + 2*pi*R^2, so the input should be h and R values of cylinder. The program code:
float area, R, h;
area = 2*3.14*R*h + 2*3.14*R*R;
that's all
o thats it? thats all that i put in my complier?
Of course, you need to write the other parts of the program to actually get it to compile and run. You also need to write the part that reads in input and prints the output.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Begin with the pseudo-code which apparently you have to write anyway.
The formula for this only involves two variable, radius and height. You don't need <math.h>, but you do need pi, which you can just define this way:
I would say the pseudo-code involves slightly more than just the formula, btwCode:#define PI 3.1415
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
#include<stdio.h>
#include<math.h>
/*prompt the user for the radius
get the radius
prompt the user for the height
get the height
compute the surface area of the cylinder
display the value of radius, height and the calculated surface area
*/
int main ()
{
int r;
int h;
double sa;
printf("Enter a value for the radius:");
scanf("%d",&r);
printf("Enter a value for the height:");
scanf("%d",&h);
sa= 2 * M_PI * (r * r) + 2 * M_PI * r * h;
printf( "radius is %d\n", r);
printf( "height is %d\n", h);
printf( "surface area is %f\n", sa);
return (0);
is this right?
I doubt if most cylinders would have a radius or a height, that would be an even integer. Better make them double.
Either put a space before the % in your second scanf() line, or add a getchar() statement, after the first scanf() call, to pull the newline that scanf() will always leave behind, off the keyboard buffer.
Second scanf() should be skipped unless you do.
Also, use code tags on our forum, please. Just highlight the program code, and click on the "#" mark on the advanced reply window.
Also, I'd add a getchar() or scanf() or something, on the line before the return, to hold the console window open long enough to get your screenshot, unless you redirect it, anyway.
Last edited by Adak; 03-12-2009 at 08:55 AM.