Wednesday, September 17, 2014

program that calculates area and perimeter of a triangle, square, and circle using macro with argument


Problem

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 - Code

#include<stdio.h>
#include<conio.h>
#define CAREA(r) (3.14*r*r)
#define cperi(r) (2*3.14*r)
#define sarea(x) (x*x)
#define speri(x) (4*x)
#define tarea(x,y,z) (0.5*x*y)
#define yperi(x,y,z) (x+y+z)
main ()
{
 float r,s,b,a,h,car,tar,sar,cpr,tpr,spr,x;

 printf ("\n for a circle");
 printf ("\n \n enter radius:");
 scanf ("%f",&r);

 car = CAREA (r);
 cpr = cperi (r);
 printf ("area = %f", car);
 printf ("\n perimeter =%f", cpr);

 printf ("\n \n for a square:");

 printf ("\n \n enter side: ");
 scanf ("%f", &s);

 sar = sarea(s);
 spr = speri (s);

 printf ("\n area =%f", sar);
 printf ("\n perimeter =%f", spr);

 printf ("\n \n for a triangle");

 printf ("\n enter base, altitude, height:");
 scanf ("%f %f %f ", &b, &a, &h);

 tar = tarea (b, a, h);
 tpr = yperi (b, a, h);

 printf ("\n area=%f", tar);
 printf ("\n perimeter= %f", tpr);

 printf ("\n\npress any key to close.");
 getch();
}

Sample Output

use macro with argument to calculate area and peritmeter

No comments :

Post a Comment

Note: Only a member of this blog may post a comment.

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact