Wednesday, September 17, 2014

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

0 comments

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

Read More..

Defining macro's in c language

0 comments

Problem

Write down macro definitions for the following:
  1. To test whether a character entered is a small case letter or not.
  2. To test whether a character entered is a upper case letter or not.
  3. To test whether a character is an alphabet or not. Make use of the macros you defined in (1) and (2) above.
  4. To obtain the bigger of two numbers.

Solution - Code (a)

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#define LOWER(ch) (ch>=97&&ch<=122 ? printf("\nlower"):printf("\n not lower"))
main()
{
 char ch;

 printf("\n enter the character=");
 scanf("%c",&ch);

 LOWER (ch);

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

Sample Output (a)


character is lower case or not

Solution - Code (b)

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#define UPPER(ch) (ch>=65&&ch<=90 ? printf("\nUpper case"):printf("\n not upper case"))
main()
{
 char ch;

 printf("\n enter the character=");
 scanf("%c",&ch);

 UPPER (ch);

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

Sample Output (b)


character is upper case or not

Solution - Code (c)

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#define ALPHA(ch) (isalpha (ch) ? printf("\n alphabet"):printf("\n not alphabet"))
main()
{
 char ch;

 printf("\n enter the character=");
 scanf("%c",&ch);

 ALPHA (ch);

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

Sample Output (c)


character is alphabet or not

Solution - Code (d)

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#define LARGE(a,b) (a>b ? printf("\n a is greater"):printf("\n b is greater"))
main()
{
 int a,b;

 printf("\n enter the two number =");
 scanf("%d%d",&a,&b);

 LARGE (a,b);

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

Sample Output (d)


bigger of two numbers

Read More..

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