Monday, 19 August 2013

chapter 7 solutions problem [C][b]

Question-Write macro definitions with arguments for calculation of 
area and perimeter of a triangle, a square and a circle. Store 
these macro definitions in a file called “areaperi.h”. Include 
this file in your program, and call the macro definitions for 
calculating area and perimeter for different squares, triangles 
and circles. 

Solution-
#include<stdio.h>
#include<math.h>
#define pi 3.14
#define areacircle(r) (pi*r*r);
#define pericircle(r) (2*pi*r);
#define areasquare(a) (a*a);
#define perisquare(a) (4*a);
#define peritraingle(d,b,c) (d+b+c);
#define s (peris/2)
#define areatraingle(a,b,c) (sqrt(s*(s-a)*(s-b)*(s-c)))

int main()
{
float r,a,b,c,d,arc,art,ars,peric,peris,perit;
printf("Enter the value of radius\n");
scanf("%f",&r);
arc=areacircle(r)
printf("Area of the circle=%f\n",arc);
peric=pericircle(r)
printf("Perimeter of the circle=%f\n",peric);
printf("Enter the length of the side of the square\n");
scanf("%f",&a);
ars=areasquare(a)
printf("Area of the square=%f\n",ars);
peris=perisquare(a)
printf("Perimeter of the square=%f\n",peris);
printf("Enter the value of b,c,d(sides of the traingle\n");
scanf("%f%f%f",&b,&c,&d);
perit=peritraingle(d,b,c)
printf("Perimeter of the triangle=%f\n",perit);
art=areatraingle(d,b,c);
printf("Area of the traingle=%f\n",art);
return 0;
}

/*Here note that the part with yellowish colour should be saved as a header file and remaining part should be written in different file. we just need to include the header file like #include<filename.h> or #include"finename.h". */

No comments:

Post a Comment