Using Macros Find Area and Perimeter of Triangle, Square, Circle: C Program


Problem State: 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.

Related Read:
Switch Case Default In C Programming Language
Macros With Arguments: C Program
do-while Loop In C Programming Language

Problem Statement Analysis

1. We need to write macro definitions which accept arguments.
2. We need to write macro definitions to calculate area and perimeter of triangle, square and circle.
3. We need to write macro definition inside a separate file called areaperi.h and then include this header file into our source file and make use of the macro definitions to calculate area and perimeter of Triangle, Square and Circle for various user inputs.

Video Tutorial: Using Macros Find Area and Perimeter of Triangle, Square, Circle: C Program


[youtube https://www.youtube.com/watch?v=lR50ONlpDNw]

YouTube Link: https://www.youtube.com/watch?v=lR50ONlpDNw [Watch the Video In Full Screen.]

Source Code: Using Macros Find Area and Perimeter of Triangle, Square, Circle: C Program

areaperi.h

#include<math.h>

#define TRI_PERI(a, b, c) (a + b + c)
#define SP(a, b, c) ( (a + b + c) / 2.0 )
#define TRI_AREA(a, b, c) ( sqrt( SP(a,b,c) * \
                                 ( SP(a,b,c) - a ) *\
                                  (SP(a,b,c) - b) * \
                                  (SP(a,b,c) - c)) )

#define SQR_PERI(s) (4 * s)
#define SQR_AREA(s) (s * s)

#define C_PERI(r) (2 * M_PI * r)
#define C_AREA(r) (M_PI * r * r)

main.c

#include<stdio.h>
#include "areaperi.h"

int main()
{
    float a, b, c, side, radius;
    int choice, repeat;

    do
    {
        printf("1. Area of Triangle\n");
        printf("2. Perimeter of Triangle\n");
        printf("3. Area of Square\n");
        printf("4. Perimeter of Square\n");
        printf("5. Area of Circle\n");
        printf("6. Perimeter of Circle\n");

        printf("\nEnter your choice\n");
        scanf("%d", &choice);

        switch(choice)
        {
            case 1: printf("Enter 3 sides of a Triangle\n");
                    scanf("%f%f%f", &a, &b, &c);
                    printf("Area of Trianlge is %0.2f\n", TRI_AREA(a,b,c));
                    break;
            case 2: printf("Enter 3 sides of a Triangle\n");
                    scanf("%f%f%f", &a, &b, &c);
                    printf("Perimeter of Trianlge is %0.2f\n", TRI_PERI(a,b,c));
                    break;
            case 3: printf("Enter length of side of Square\n");
                    scanf("%f", &side);
                    printf("Area of Square is %0.2f", SQR_AREA(side));
                    break;
            case 4: printf("Enter length of side of Square\n");
                    scanf("%f", &side);
                    printf("Perimeter of Square is %0.2f", SQR_PERI(side));
                    break;
            case 5: printf("Enter Radius of Circle\n");
                    scanf("%f", &radius);
                    printf("Area of Circle is %0.2f\n", C_AREA(radius));
                    break;
            case 6: printf("Enter Radius of Circle\n");
                    scanf("%f", &radius);
                    printf("Circumference of Circle is %0.2f\n", C_PERI(radius));
                    break;
            default: printf("\nPlease enter valid choice\n");
        }

        printf("\n\nDo you want to continue? Ans: 0 or 1\n");
        scanf("%d", &repeat);

        printf("\n");

    }while(repeat);

    return 0;
}

Output:
1. Area of Triangle
2. Perimeter of Triangle
3. Area of Square
4. Perimeter of Square
5. Area of Circle
6. Perimeter of Circle

Enter your choice
1
Enter 3 sides of a Triangle
5
6
9
Area of Trianlge is 14.14

Do you want to continue? Ans: 0 or 1
1

1. Area of Triangle
2. Perimeter of Triangle
3. Area of Square
4. Perimeter of Square
5. Area of Circle
6. Perimeter of Circle

Enter your choice
2
Enter 3 sides of a Triangle
5
6
9
Perimeter of Trianlge is 20.00

Do you want to continue? Ans: 0 or 1
1

1. Area of Triangle
2. Perimeter of Triangle
3. Area of Square
4. Perimeter of Square
5. Area of Circle
6. Perimeter of Circle

Enter your choice
3
Enter length of side of Square
5
Area of Square is 25.00

Do you want to continue? Ans: 0 or 1
1

1. Area of Triangle
2. Perimeter of Triangle
3. Area of Square
4. Perimeter of Square
5. Area of Circle
6. Perimeter of Circle

Enter your choice
4
Enter length of side of Square
5
Perimeter of Square is 20.00

Do you want to continue? Ans: 0 or 1
1

1. Area of Triangle
2. Perimeter of Triangle
3. Area of Square
4. Perimeter of Square
5. Area of Circle
6. Perimeter of Circle

Enter your choice
5
Enter Radius of Circle
5.5
Area of Circle is 95.03

Do you want to continue? Ans: 0 or 1
1

1. Area of Triangle
2. Perimeter of Triangle
3. Area of Square
4. Perimeter of Square
5. Area of Circle
6. Perimeter of Circle

Enter your choice
6
Enter Radius of Circle
5.5
Circumference of Circle is 34.56

Do you want to continue? Ans: 0 or 1
0

Formulas To Calculate Area and Perimeter

We’ve written the formula to calculate area and perimeter inside areaperi.h file, as macro expansion. Below we list all the formulas we’re using in our program:

Perimeter of a Triangle: ( a + b + c)
where: a, b, and c are lengths of sides of the triangle.

Semi-perimeter of a Triangle: ( (a + b + c) / 2 )
where: a, b, and c are lengths of sides of the triangle.

Area of a Triangle: sqrt( S x (S – a) x (S – b) x (S – c) )
where: a, b, and c are lengths of sides of the triangle.
S is the semi-perimeter.

Perimeter of Square: ( 4 x side )
where: side is the length of side of the square.

Area of Square: ( side x side)
where: side is the length of side of the square.

Perimeter or Circumference of Circle: ( 2 x PI x r )
Area of a Circle: ( PI x r x r )
where: r is radius of the circle.
PI is approximately equal to 3.14

Note: M_PI is a macro present inside math.h library file and has value of PI.

Related Read:
C Program To Find Area, Perimeter and Semi-perimeter: Valid Triangle
C Program To Calculate Perimeter, Diagonal of a Square using its Side
C Program To Find Area and Circumference of Circle using Pointer

Logic

We’ve included areaperi.h into main.c file. Now we can make use of all the macros we’ve defined inside areaperi.h

Users are provided with options to select the preferred operations like:
1. Area of Triangle
2. Perimeter of Triangle
3. Area of Square
4. Perimeter of Square
5. Area of Circle
6. Perimeter of Circle

Based on user selection appropriate block of code inside switch-case is executed, wherein we place the relevant macro template to get the desired result.

Note: We can continue writing macro expansion in next line by making use of macro continuation operator(\). You can see that we’ve broken the line and written the code in next line inside macro expansion of TRI_AREA.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

Your email address will not be published. Required fields are marked *