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

C Program To Find Area and Circumference of Circle using Pointer

Lets write a C program to calculate area and circumference or perimeter of a Circle using pointer and function.

Related Read:
Function / Methods In C Programming Language
Basics of Pointers In C Programming Language

Video Tutorial: C Program To Find Area and Circumference of Circle using Pointer


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

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


Source Code: C Program To Find Area and Circumference of Circle using Pointer

#include<stdio.h>

void area_peri(float, float*, float*);

int main()
{
    float radius, area, perimeter;

    printf("Enter radius of Circle\n");
    scanf("%f", &radius);

    area_peri(radius, &area, &perimeter);

    printf("\nArea of Circle = %0.2f\n", area);
    printf("Perimeter of Circle = %0.2f\n", perimeter);

    return 0;
}

void area_peri(float r, float *a, float *p)
{
    *a = 3.14 * r * r;
    *p = 2 * 3.14 * r;
}

Output 1:
Enter radius of Circle
5

Area of Circle = 78.50
Perimeter of Circle = 31.40

Output 2:
Enter radius of Circle
14

Area of Circle = 615.44
Perimeter of Circle = 87.92

Logic To Find Area and Circumference of Circle using Pointer

We ask the user to enter value for radius of a Circle. We pass this value along with address of variables area and perimeter to the function area_peri().

area_peri(radius, &area, &perimeter);

We copy the value of radius to a local variable r and then we take 2 floating point pointer variables *a and *p. *a represents the value present at address a or &area. *p has value present at address p or &perimeter.

Inside area_peri() function we calculate the area and circumference / perimeter of Circle and store it as value present at addresses a and p. Since a points to address of variable area and p points to address of variable perimeter, the values of variable area and perimeter changes too.

*a = 3.14 * r * r;

*p = 2 * 3.14 * r;

Area and Circumference of Circle

We’ve separate video tutorials to calculate area and circumference of a Circle using radius, and without using pointer and function. You can check them out at these links:

Calculate Area of a Circle without using math.h library: C
C Program To Calculate Circumference of Circle

Note: When * is precedes any address, it fetches the value present at that address or memory location.

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

C Program To Find Whether Area of Rectangle Is Greater Than Its Perimeter

Given the length and breadth(or width) of a rectangle, write a C program to find whether the area of the rectangle is greater than its perimeter. For example, the area of the rectangle with length = 5 and breadth = 4 is greater than its perimeter.

Related Read:
Area of Rectangle: C Program
Perimeter of Rectangle: C Program

Note:
Rectangle is a quadrilateral(i.e., it has 4 sides). And all the 4 angles of the rectangle are of 90 degree. To calculate area of a rectangle we need its length and width/breadth.

Formula To Calculate Area of a Rectangle

Area = Length * Width;
area of rectangle

Formula To Calculate Perimeter of a Rectangle

Perimeter = 2 * (Length + Width);
perimeter of rectangle

Length: is the length of the longest side of the rectangle.
width: is the length of the smallest side of the rectangle.

Expected Output for the Input

User Input:
Enter length of the Rectangle
5
Enter width of the Rectangle
4

Output:
yes, area(20.00) is greater than its perimeter(18.00)

Video Tutorial: C Program To Find Whether Area of Rectangle Is Greater Than Its Perimeter


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

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

Source Code: C Program To Find Whether Area of Rectangle Is Greater Than Its Perimeter

#include < stdio.h >

int main()
{
    float length, width, area, perimeter;

    printf("Enter length of the Rectangle\n");
    scanf("%f", &length);

    printf("Enter width of the Rectangle\n");
    scanf("%f", &width);

    area      =     (length * width);
    perimeter = 2 * (length + width);

    if(area > perimeter)
    {
        printf("yes, area(%0.2f) is greater than its perimeter(%0.2f)\n", 
               area, perimeter);
    }
    else
    {
        printf("Area(%0.2f) is not greater than its perimeter(%0.2f)\n", 
               area, perimeter);
    }

    return 0;
}

Output:
Enter length of the Rectangle
12
Enter width of the Rectangle
6
yes, area(72.00) is greater than its perimeter(36.00)

Logic To Find Whether Area of Rectangle Is Greater Than Its Perimeter

We ask the user to enter length and breadth/width of the rectangle and store it inside address of variable length and width. Next we calculate area and perimeter of the rectangle using the user entered value for length and width of the rectangle.

Area = Length * Width;
Perimeter = 2 * (Length + Width);

Next we check if the value present in variable area is greater than the value of perimeter or not, and output the result accordingly to the console window.

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

C Program To Calculate Perimeter, Diagonal of a Square using its Side

Lets write a C program to calculate area, perimeter and diagonal of a square by using length of its side. We ask the user to input the length of its side.

Related Read:
C Program To Calculate Area of a Square using its Side
C Program To Calculate Area of a Square using its Diagonal

Formula To Calculate Area, Perimeter and Diagonal of a Square using its side

diagonal = sqrt(2) x side;

area = ( diagonal x diagonal ) / 2.0
OR
area = ( diagonal x diagonal ) * 0.5
OR
area = side x side;

Perimeter = 4 x side;

Note: All the sides of a square are equal. Both the diagonals of the square are of equal length.

diagonal of square using its side

Expected Output for the Input

User Input:
Enter length of side of the Square
10.5

Output:
Area of the Square is 110.250000
Perimeter of the Square is 42.000000
Diagonal of the Square is 14.849242

Video Tutorial: C Program To Calculate Perimeter, Diagonal of a Square using its Side


[youtube https://www.youtube.com/watch?v=QXO-lF-EbvA]

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

Source Code: C Program To Calculate Perimeter, Diagonal of a Square using its Side

#include < stdio.h >
#include < math.h >

int main()
{
    float perimeter, diagonal, side, area;

    printf("Enter length of side of the Square\n");
    scanf("%f", &side);

    perimeter = 4 * side;
    diagonal  = sqrt(2) * side;
    area      = side * side;

    printf("Area of the Square is %f\n", area);
    printf("Perimeter of the Square is %f\n", perimeter);
    printf("Diagonal of the Square is %f\n", diagonal);

    return 0;
}

Output 1:
Enter length of side of the Square
10
Area of the Square is 100.000000
Perimeter of the Square is 40.000000
Diagonal of the Square is 14.142136

Output 2:
Enter length of side of the Square
5
Area of the Square is 25.000000
Perimeter of the Square is 20.000000
Diagonal of the Square is 7.071068

Output 3:
Enter length of side of the Square
12.2
Area of the Square is 148.839996
Perimeter of the Square is 48.799999
Diagonal of the Square is 17.253405

Output 4:
Enter length of side of the Square
10.5
Area of the Square is 110.250000
Perimeter of the Square is 42.000000
Diagonal of the Square is 14.849242

Output 5:
Enter length of side of the Square
3.2
Area of the Square is 10.240001
Perimeter of the Square is 12.800000
Diagonal of the Square is 4.525484

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

Find Area of an Equilateral Triangle: C Program

Given the sides of the Triangle, write a C program to calculate Perimeter and Area of a equilateral Triangle.

Related Read:
Find Area of a Triangle Using Its Sides: C Program

Note:
Equilateral Triangle: A Triangle is called equilateral triangle if length of 3 sides of it are equal.
Example: a = 10, b = 10, c = 10;

So we ask the user to input value of the side only once.

Formula To Calculate Perimeter and Area of Equilateral Triangle

Perimeter = a + a + a;
i.e., Perimeter = 3 * a;

Area = ( ( sqrt(3) / 4 ) * pow(a, 2));

where a is the value of side of the Equilateral Triangle.

Find Area of an Equilateral Triangle: C Program


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

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


Find Area of an Equilateral Triangle: C Program

#include < stdio.h >
#include < math.h >

int main()
{
    float sides, area, p;

    printf("Enter value for side of Equilateral Triangle\n");
    scanf("%f", &sides);

    p    = 3 * sides;
    area = ( (sqrt(3)/4) * pow(sides, 2) );

    printf("Perimeter is %f\n", p);
    printf("Area is %f\n", area);

    return 0;
}

Output:
Enter value for side of Equilateral Triangle
75
Perimeter is 225.000000
Area is 2435.696533

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