ccplusplus.com
Learn C, C++ Concepts
Saturday, September 24, 2011
c program to calculate area of cuboid
/**************************************************************** * File : calculate-area.c * Author : Saurabh Gupta * Desc : calculate area, Perimeter of the cuboid c program * Source : http://saurabhgupta0527.blogspot.com/p/c.html * Created : PM 12:29 24 September 2011 * Note : 1. Argument validation is not yet done. Please * do add that code if you are playing with that * 2. you need to link it to math library (-lm) see * below linking at bottom of program ****************************************************************/ #include <stdio.h> #include <math.h> #include <unistd.h> #include <stdlib.h> #define PI 3.14159 /* * local function declarations */ char menu(); int shape_square(); int shape_rectangle(); int shape_triangle (); int shape_rhombus(); int shape_circle(); int shape_parallalogram(); int shape_trapezium(); int shape_quadrilateral(); int shape_semicircle(); int shape_sector(); int shape_sphere(); int shape_cone(); int shape_cyllinder(); int shape_cube(); int shape_cuboid(); int shape_hemisphe(); char ch; // making it global for all the functions int main() { printf ("\n"); printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); printf ("XXXXXXXX AREA / VOLUME / CIRCUMFERENCE Calculator XXXXXX\n"); printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); ch = menu(); // lets take what user want to do switch(ch) { case 'a': case 'A': shape_square(); break; case 'b': case 'B': shape_rectangle(); break; case 'c': case 'C': shape_circle(); break; case 'd': case 'D': shape_triangle(); break; case 'e': case 'E': shape_rhombus(); break; case 'f': case 'F': shape_parallalogram(); break; case 'g': case 'G': shape_trapezium(); break; case 'h': case 'H': shape_quadrilateral(); break; case 'i': case 'I': shape_semicircle(); break; case 'j': case 'J': shape_sector(); break; case 'k': case 'K': shape_sphere(); break; case 'l': case 'L': shape_cone(); break; case 'm': case 'M': shape_cyllinder(); break; case 'n': case 'N': shape_cube(); break; case 'o': case 'O': shape_cuboid(); break; case 'p': case 'P': shape_hemisphe(); break; case 'q': case 'Q': exit(1); } printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); return 0; } char menu() { printf ("\t\t\tOPTION MENU\n"); printf ("Two Dimensional Shapes\n"); printf ("A. SQUARE\n"); printf ("B. RECTANGLE\n"); printf ("C. CIRCLE\n"); printf ("D. TRIANGLE\n"); printf ("E. RHOMBUS\n"); printf ("F. PARALLELOGRAM\n"); printf ("G. TRAPEZIUM\n"); printf ("H. QUADRILATERAL.\n"); printf ("I. SEMICERCLE \n"); printf ("J. SECTOR\n"); printf ("\nThree Dimensional Shapes Menu\n"); printf ("K. SPHERE \n"); printf ("L. CONE \n"); printf ("M. CYLLINDER \n"); printf ("N. CUBE \n"); printf ("O. CUBOID\n"); printf ("P. HEMISPHERE\n"); printf ("Q. QUIT\n"); printf ("\nEnter Your Choice :\n"); scanf("%c", &ch); return (ch); } /* * SUB FUNCTIONS * 2 DIMENSIONAL SHAPES */ int shape_square() { float s, a, p; int i, j; printf ("Enter side of square : \n"); scanf ("%f", &s); a = s*s; p = 4*s; printf ("Perimeter of square : %.3f units\n",p); printf ("Area of square : %.3f sq.units\n",a); return(0); } int shape_rectangle() { float a,p,l,b; int i,j; printf ("Enter length and breadth of rectangle.\n"); printf ("Length : "); scanf ("%f", &l); printf ("Breadth : "); scanf ("%f",&b); a = l*b; p = 2*(l+b); printf ("Perimeter of rectangle : %.3f units\n",p); printf ("Area of rectangle : %.3f sq.units\n",a); return(0); } int shape_triangle() { float area, p; float a, b, c, s; printf ("Enter three sides of triangle : "); scanf ( "%f%f%f",&a,&b,&c); p = a+b+c; s = p/2; area = sqrt(s*(s-a)*(s-b)*(s-c)); printf ("Perimeter of triangle : %.3f units\n",p); printf ("Area of a triangle : %.3f sq.units\n",area); return 0; } int shape_rhombus() { float s,d1,d2,a,p; printf ("Enter side and diagonals of a rhombus.\n"); printf ("Side : "); scanf ("%f",&s); printf ("First Diagonal :"); scanf ("%f",&d1); printf ("Second Diagonal :"); scanf ("%f",&d2); a = 0.5*d1*d2; p = 4*s; printf ("Perimeter of rhombus :%.3f units\n",p); printf ("Area of rhombus :%.3f sq.units\n",a); return 0; } int shape_circle() { float r,a,p; printf ("Enter radius of circle : "); scanf ("%f", &r); a=PI * r * r; p=2 * PI * r; printf ("Circumference of circle : %.3f units\n",p); printf ("Area of circle : %.3f sq.units\n",a); return 0; } int shape_parallalogram() { float a,p,base,h,l,b; printf ("Enter height,length,breadth of parallalogram :\n"); printf ("Height :\n"); scanf ("%f", &h); printf ("Base or Length\n"); scanf ("%f",&l); printf ("Breadth :\n"); scanf ("%f",&b); base = l; a = base*h; p = 2 * ( l + b ); printf ("Perimeter of parallalogram :%.3f units\n",p); printf ("Area of parallogram :%.3f sq.units\n",a); return 0; } int shape_trapezium() { float a, b, d, are; printf ("Enter height and lengths of two parallel sides\n"); printf ("Height : "); scanf ("%f", &d); printf ("Side : "); scanf ("%f", &a); printf ("Side : "); scanf ("%f", &b); are = 0.5 * d * (a+b); printf ("Area of trapezium : %.3f sq.units\n",are); return 0; } int shape_quadrilateral() { float a,b,area,d; printf ("Enter diagonal and perpendicular distances from opposite vertices\n"); printf ("Diagonal :\n"); scanf ("%f", &d); printf ("Distance :\n"); scanf ("%f", &a); printf (" Distance :\n"); scanf ("%f", &b); area = 0.5 * d * (a + b); printf ("Area of quadrilateral : %.3f sq.units\n", area); return 0; } int shape_semicircle() { float a,p,r; printf ("Enter radius of semicircle:"); scanf ("%f",&r); a=0.5* PI * r * r; p= (PI * r ) + (2 * r); printf ("Circumference of semicircle : %.3f units\n",p); printf ("Area of semicircle : %.3f sq.units\n",a); return 0; } int shape_sector() { float x,r,temp,a,p; printf ("Enter radius and angle of sector.\n"); printf ("Radius : "); scanf ("%f", &r); printf ("Angle(in degrees) : "); scanf ("%f", &x); temp = x/360; a = temp * (PI * r * r); p = temp * (2 * PI * r); printf ("Circumference of sector : %.3f units\n",p); printf ("Area of sector : %.3f sq.units\n",a); return 0; } /* * SUB FUNCTIONS * 3 DIMENSIONAL SHAPES */ int shape_sphere() { float lsa,tsa,v,r; printf("Enter radius of sphere :"); scanf("%f",&r); tsa=4*PI*r*r; v=(4.0/3.0)*PI*r*r*r; printf("Total surface area of sphere :%.3f sq.units\n",tsa); printf("Volume of sphere :%.3f cu.units\n",v); return 0; } int shape_cone() { float h,r,s ,v,tsa,lsa; printf ("Enter base radius ,height, slant height of cone\n"); printf ("Radius : "); scanf ("%f",&r); printf ("Height : "); scanf ("%f",&h); printf ("Slant height :"); scanf ("%f",&s); tsa = PI * r *(s+r); lsa = PI * r * s; v = (PI * r * r * h)/3; printf ("Total surface area of cone :%.3f sq.units\n",tsa); printf ("Lateral surface area of cone :%.3f sq.units\n",lsa); printf ("Volume of cone :%.3f cu.units\n",v); return 0; } int shape_cyllinder() { float lsa,tsa,v,r,h; printf ("Enter height and radius of cyllinder\n"); printf ("Height :"); scanf ("%f",&h); printf ("Radius :"); scanf ("%f",&r); lsa = 2*PI*r*h; tsa = 2*PI*r*(h+r); v = PI*r*r*h; printf ("Total surface area of cyllinder :%.3f sq.units\n",tsa); printf ("Curved surface area of cyllinder :%.3f sq.units\n",lsa); printf ("Volume of cyllinder :%.3f cu.units\n",v); return 0; } int shape_cube() { float lsa,tsa,v,s,d; printf ("Enter side of cube\n"); scanf ("%f", &s); d = s*sqrt(3); lsa = 4 * s * s; tsa = 6 * s * s; v = s * s * s; printf ("Diagonal of cube :%.3f units\n",d); printf ("Total surface area of cube :%.3f sq.units\n",tsa); printf ("Lateral surface area of cube :%.3f sq.units\n",lsa); printf ("Volume of cube :%.3f cu.units\n",v); return 0; } int shape_cuboid() { float lsa,tsa,v,l,b,d,h; printf ("Enter length,breadth,height of cuboid :\n"); printf ("Length :"); scanf("%f",&l); printf ("Breadth :"); scanf("%f",&b); printf ("Height :"); scanf("%f",&h); d = sqrt(l*l + b*b + h*h ); lsa =2 * h *( l+b ); tsa = lsa + 2 * l * b; v = l*b*h; printf ("Diagonal of cuboid :%.3f units\n",d); printf ("Total surface area of cuboid :%.3f sq.units\n",tsa); printf ("Lateral surface area of cuboid :%.3f sq.units\n",lsa); printf ("Volume of cuboid :%.3f cu.units\n",v); return 0; } int shape_hemisphe() { float lsa,tsa,v,r; printf("Enter radius of hemisphere :\n"); scanf("%f",&r); tsa = 3*PI*r*r; lsa = 2*PI*r*r; v = (2.0/3.0)*PI*r*r*r; printf("Total surface area of hemisphere :%.3f sq.units\n",tsa); printf("Lateral surface area of hemisphere :%.3f sq.units\n",lsa); printf("Volume of hemisphere :%.3f cu.units\n",v); return 0; } /* * OUTPUT * [sgupta@rhel55x86 c]$ gcc -o calculate-area -lm calculate-area.c [sgupta@rhel55x86 c]$ ./calculate-area XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXX AREA / VOLUME / CIRCUMFERENCE Calculator XXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX OPTION MENU Two Dimensional Shapes A. SQUARE B. RECTANGLE C. CIRCLE D. TRIANGLE E. RHOMBUS F. PARALLELOGRAM G. TRAPEZIUM H. QUADRILATERAL. I. SEMICERCLE J. SECTOR Three Dimensional Shapes Menu K. SPHERE L. CONE M. CYLLINDER N. CUBE O. CUBOID P. HEMISPHERE Q. QUIT Enter Your Choice : o Enter length,breadth,height of cuboid :Length :3 Breadth :4 Height :5 Diagonal of cuboid :7.071 units Total surface area of cuboid :94.000 sq.units Lateral surface area of cuboid :70.000 sq.units Volume of cuboid :60.000 cu.units XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX [sgupta@rhel55x86 c]$ */
See also
Other popular tricky C Sample Codes and language Concept
.
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment