Chapter 7 : The C Preprocessor (Let us 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.
(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.
(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.
(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.
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.
// Let Us C (Chapter 7 : The C Preprocessor) : Program-1
/* 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 */
#include<stdio.h>
#include<conio.h>
#define SMALL(ch) ((ch>=97)&&(ch<=122))
#define CAP(ch) ((ch>=65) && (ch<=90))
#define ALPHA(ch) (SMALL(ch)||CAP(ch))
#define BIGGER(a,b) (a>b)?a:b
void main()
{
unsigned int Choice;
int a,b,bigger;
char c;
clrscr();
printf("1:To test whether a character entered is a small case letter or not\n");
printf("2:To test whether a character entered is a upper case letter or not\n");
printf("3:To test whether a character is an alphabet or not\n");
printf("4:To obtain the bigger of two numbers\n");
printf("Enter your choice: ");
scanf("%d",&Choice);
switch(Choice)
{
case 1:
printf("Enter a character: ");
scanf(" %c",&c); //space to discard whitespace before char
if(SMALL(c))
printf("%c is a Small case letter",c);
else
printf("%c is Not a Small case letter",c);
break;
case 2:
printf("Enter a character: ");
scanf(" %c",&c); //space to discard whitespace before char
if(CAP(c))
printf("%c is an Upper case letter",c);
else
printf("%c is Not an Upper case letter",c);
break;
case 3:
printf("Enter a character: ");
scanf(" %c",&c); //space to discard whitespace before char
if(ALPHA(c))
printf("%c is an Alphabet",c);
else
printf("%c is Not an Alphabet",c);
break;
case 4:
printf("Enter 2 numbers: ");
scanf("%d%d",&a,&b);
bigger = BIGGER(a,b);
printf("%d is Bigger",bigger);
break;
default:
break;
}
}
(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.
// Let Us C (Chapter 7 : The C Preprocessor) : Program-2
/* 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. */
/********************************* areaperi.h **********************************/
//Create file areaperi.h. Save it to the same folder where .c file have been saved. compile it first and include it in .c file
#include <math.h>
#ifndef WHATEVER_H_INCLUDED
/*They avoid including the same header file twice in the same compilation*/
#define WHATEVER_H_INCLUDED
#define PI 3.14
#define TRI_AREA(a,b,c,s) (sqrt(s*(s-a)*(s-b)*(s-c)))
#define TRI_PERI(a,b,c) (a+b+c)
#define SQ_AREA(s) (s*s)
#define SQ_PERI(s) (4*s)
#define CIRC_AREA(r) (PI*r*r)
#define CIRC_PERI(r) (2*PI*r)
#endif
/********************************* .c file **********************************/
#include<stdio.h>
#include<conio.h>
#include "areaperi.h" /* inclusion header file */
void main()
{
int Choice;
float a,b,c,s,tri_area,tri_peri,sq_area,sq_peri,circ_area,circ_peri;
clrscr();
printf("Program to calculate Area & Perimeter\n");
printf("1: TRIANGLE\n");
printf("2: SQUARE\n");
printf("3: CIRCLE\n");
printf("Enter your choice: ");
scanf("%d",&Choice);
switch(Choice)
{
case 1:
printf("Enter 3 sides of the Triangle: ");
scanf("%f%f%f,&a,&b,&c");
s = (a+b+c)/2;
tri_area = TRI_AREA(a,b,c,s);
tri_peri = TRI_PERI(a,b,c);
printf("\nArea of Triagnle = %0.2f",tri_area);
printf("\nPerimeter of Triagnle = %0.2f",tri_peri);
break;
case 2:
printf("Enter sides of the Square: ");
scanf("%f,&a");
sq_area = SQ_AREA(a);
sq_peri = SQ_PERI(a);
printf("\nArea of Square = %0.2f",sq_area);
printf("\nPerimeter of Square = %0.2f",sq_peri);
break;
case 3:
printf("Enter radius of the circle: ");
scanf("%f,&a");
circ_area = CIRC_AREA(a);
circ_peri = CIRC_PERI(a);
printf("\nArea of Circle = %0.2f",circ_area);
printf("\nPerimeter of Circle = %0.2f",circ_peri);
break;
}
}
(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.
// Let Us C (Chapter 7 : The C Preprocessor) : Program-3
/* 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 */
#include<stdio.h>
#include<conio.h>
#define AM(a,b) ((a+b)/2)
#define ABS(a) (a>0)?a:(a*(-1))
#define UtoL(ch) ((ch>64)&&(ch<91))?(ch+32):0
#define BIGGER(a,b) ((a>b)?a:b)
void main()
{
int Choice,num,abs;
float a,b,am,bigger;
char ch,utol;
clrscr();
printf("1: Arithmetic Mean\n");
printf("2: Absolute Value\n");
printf("3: UpperCase to LowerCase\n");
printf("4: Bigger of 2 numbers\n");
printf("Enter your choice: ");
scanf("%d",&Choice);
switch(Choice)
{
case 1:
printf("Enter 2 numbers: ");
scanf("%f%f",&a,&b);
am = AM(a,b);
printf("\nArithmetic Mean of %0.2f & %0.2f = %0.2f",a,b,am);
break;
case 2:
printf("Enter a numbers: ");
scanf("%d",&num);
abs = ABS(num);
printf("\nAbsolute value of %d is %d",num,abs);
break;
case 3:
printf("Enter an UpperCae alphabet: ");
scanf(" %c",&ch); //space included to discard any whitespace
utol = UtoL(ch);
if(utol)
printf("\n%c in Uppercase = %c in Lowercase",ch,utol);
else
printf("You did not Enter Uppercase alphabet");
break;
case 4:
printf("Enter 2 numbers: ");
scanf("%f%f",&a,&b);
bigger = BIGGER(a,b);
printf("\n%0.2f is bigger",bigger);
break;
}
}
(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.
// Let Us C (Chapter 7 : The C Preprocessor) : Program-4
/* 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. */
#include<stdio.h>
#include<conio.h>
#include"interest.h" /* inclusion of header file */
void main()
{
int p,r,t,a;
float si;
clrscr();
printf("Program to calculate Simple Interest\n");
printf("Enter the Principle Amount: ");
scanf("%d",&p);
printf("Enter the rate of interest(per year): ");
scanf("%d",&r);
printf("Enter the time(in years): ");
scanf("%d",&t);
si=INTEREST(p,r,t);
a=AMOUNT(p,si);
printf("\nsimple interest = %f\namount = %d",si,a);
}
/********************************* interest.h **********************************/
Create file interest.h. Save it to the same folder where .c file have been saved. compile it first
& include it in .c file
#define INTEREST(x,y,z) (x*y*z/100)
#define AMOUNT(x,y) (x+y)
nice
ReplyDelete