Let Us C / Chapter 7 (The C Pre-processor)

                              Exercise [C]


(a) 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:


#include<stdio.h>
#include<conio.h>
#define UPPER(x) (x>=65 && x<=90)
#define SMALL(y) (y>=97 && y<=123)
#define ALPHABET(z) (z>=65 && z<=90 || z>=97 && z<=123)
#define BIGGER(a,b) (a>b)

void main() {

int i,d1,d2;
char ch,ch1;
clrscr();

printf("\t enter your choice: \n");
printf("\t===================\n\n");

printf("1: to test if character is small case letter or not\n\n");
printf("2: to test if character is upper case letter or not\n\n");
printf("3: to test if character is an alphabet or not\n\n");
printf("4: to find bigger of two number\n\n\n");

printf("choice: ");
scanf("%d",&i);

switch (i) {

case 1:

clrscr();

printf("enter any character\n\n");

scanf(" %c",&ch);

if SMALL(ch)
printf("\n\n it is a small case letter.\n");
else
printf("\n\nit is not a small case letter.\n");

break;

case 2:

clrscr();

printf("enter any character\n\n");

scanf(" %c",&ch);

if UPPER(ch)
printf("\n\nit is an upper case letter.\n");
else
printf("\n\nit is not an upper case letter.\n");

break;

case 3:

clrscr();

printf("enter any character\n\n");

scanf(" %c",&ch);

if ALPHABET(ch)
printf("\n\nit is an alphabet.\n");

else
printf("\n\nit is not an alphabet.\n");

break;

case 4:

clrscr();

printf("enter two numbers\n\n");

scanf("%d%d",&d1,&d2);

if BIGGER(d1,d2)

printf("\n\n%d is bigger\n",d1);

else
printf("\n\n%d is bigger \n",d2);

break;

default:

printf("\n\n\nwrong choice entered\n");


}

getch();
}

--------------------------------------------------------------------

(b) 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.

Note: write these macro definitions in a new file and save it as "areaperi.h", compile it and then you can include it during inclusion of libraries as - include"areaperi.h".


#define TP(a,b,c) (a+b+c)                                /* perimeter of triangle */
#define TA(a,b,c,d) (d*((d-a)*(d-b)*(d-c)))      /* area of triangle *
#define PS(x) (4*x)                                          /* perimeter of square */
#define SA(x) (x*x)                                           /* area of square */
#define CP(x) (2*3.14*r)                                 /* perimeter of circle */
#define CA(x) (3.14*r*r)                                  /* area of circle */


Solution:

#include"areaperi.h"        /* inclusion of custom header file */
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main() {

float t1,t2,t3,hpt;      /* sides of triangle,half of perimeter */
float ss,r;                 /* side of square, radius of circle */
int ch;                 
float tp,ta,ta1,sp,sa,cp,ca;       /* perimeter & area of triangle,square,circle */


clrscr();

printf("\tFind area and perimeter of: \n");
printf("       *****************************\n\n");

printf("1: triangle\n\n");
printf("2: square\n\n");
printf("3: circle\n\n\n\n\n");

printf("enter your choice:  ");
scanf("%d",&ch);

switch(ch)

{

case 1:

/* triangle */

     clrscr();
     printf("enter side 1 of triangle:  ");
     scanf("%f",&t1);

     printf("\n\nenter side 2 of triangle:  ");
     scanf("%f",&t2);

     printf("\n\nenter side 3 of triangle:  ");
     scanf("%f",&t3);

     hpt=t1+t2+t3/2;

     tp=TP(t1,t2,t3);
     ta=TA(t1,t2,t3,hpt);

     ta1=sqrt(ta);

     printf("\n\n\tperimeter of triangle = %f",tp);
     printf("\n\tarea of triangle = %f",ta1);

     break;

case 2:

       /* square */

     clrscr();

     printf("\n\nenter side of a square:  ");
     scanf("%f",&ss);

     sp=PS(ss);
     sa=SA(ss);

     printf("\n\n\tperimeter of square = %f",sp);
     printf("\n\tarea of square = %f",sa);

     break;

case 3:

      /* circle */

     clrscr();

     printf("\n\nenter radius of circle: ");
     scanf("%f",&r);

     cp=CP(r);
     ca=CA(r);

     printf("\n\n\tperimeter of circle = %f",cp);
     printf("\n\tarea of circle = %f",ca);

     break;


default:

       exit();
}

getch();

}



-----------------------------------------------------------------------

(c) Write down macro definitions for the following:
1. To find arithmetic mean of two numbers.
2. To find absolute value of a number.
3. To convert a uppercase alphabet to lowercase.
4. To obtain the bigger of two numbers.

Solution:



#include<stdio.h>
#include<conio.h>
#define AM(x,y) ((x+y)/2)
#define ABS(x,y) (x<y)
#define LOWER(x)(122-(90-x))
#define BIG(x,y) (x>y)


void main() {

char ch,ch1;
int choice,a,b,abs,c=0;
float am;

clrscr();

printf("\tMacro Definitions to:\n");
printf("     ***************************\n\n");

printf("\n1: Find airthmetic mean of two numbers: \n\n");
printf("2: find absolute value of a number: \n\n");
printf("3: convert an uppercase character to lowercase: \n\n");
printf("4: obtain the bigger of two numbers: \n\n\n\n\n\n\n\n\n\n");


printf("enter your choice:  ");
scanf("%d",&choice);


switch(choice) {

case 1:

      /* finding arithmetic mean of two numbers */

      clrscr();

      printf("\nenter two numbers:\n ");
      scanf("%d%d",&a,&b);
      am=AM(a,b);        /* airthmetic mean */
      printf("\nairthmetic mean = %f ",am);
      break;


case 2:

      /* finding the absolute value of a number */

      clrscr();

      printf("\nenter a number: ");
      scanf("%d",&a);

      if ABS(a,c)

      abs=a*(-1);

      else
      abs=a;

      printf("\nabsolute value = %d",abs);
      break;


case 3:

      /* converting an uppercase character to equivalent lowercase */

      clrscr();

      printf("\nenter an uppercase character: ");
      scanf(" %c",&ch);

      ch1=LOWER(ch);

      printf("\nequivalent lowercase character = %c",ch1);
      break;

case 4:

      /* finding the bigger of two numbers */

      clrscr();

      printf("\nenter two numbers:\n ");
      scanf("%d%d",&a,&b);

      if BIG(a,b)
      printf("\n\n%d is bigger.\n",a);

      else
      printf("\n\n%d is bigger.\n",b);

      break;

default:

       exit();
}


getch();

}

-----------------------------------------------------------------------

(d) Write macro definitions with arguments for calculation of Simple Interest and Amount. Store these macro definitions in a file called “interest.h”. Include this file in your program, and use the macro definitions for calculating simple interest and amount.

Note: write these macro definitions in a new file and save it as "interest.h", compile it and then you can include it during inclusion of libraries as - include"interest.h".

#define INTEREST(x,y,z) (x*y*z/100)
#define AMOUNT(x,y) (x+y)

Solution:


#include<stdio.h>
#include<conio.h>
#include"interest.h"      /* inclusion of custom header file */

void main() {

int p,r,t,a;
float si;

clrscr();

printf("enter the principle:  ");
scanf("%d",&p);

printf("\n\nenter the rate:  ");
scanf("%d",&r);

printf("\n\nenter the time:  ");
scanf("%d",&t);

si=INTEREST(p,r,t);

a=AMOUNT(p,si);

printf("\n\n\n\n\t\tsimple interest = %f\n\n\t\tamount = %d",si,a);

getch();
}

_______________________________________________________________________

1 comment:

  1. Hello sir, I am getting problem in question C, there is no warning for errors during compiling the program but when I run the program it shows error that "undefined symbol _main in module c0.ASM"

    ReplyDelete

Let Us C / Chapter 7 (The C Pre-processor)

                               Exercise [C] (a) Write down macro definitions for the following: 1. To test whether a character ente...