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 Calculate Surface Area of Cylinder

Given the values for radius and height of a Cylinder, write a C program to calculate Surface Area of the Cylinder.

Formula To Calculate Surface Area of Cylinder

Surface_Area = (2 * PI * radius * height) + (2 * PI * radius * radius);
where PI is approximately equal to 3.14

surface area of Cylinder

Related Read:
Basic Arithmetic Operations In C
Area of Rectangle: C Program
Calculate Area of a Circle without using math.h library: C
C Program To Calculate Circumference of Circle

Logic of Surface Area of Cylinder Formula


A solid Cylinder has 2 circles. One at the top and one at the bottom. Area of a Circle is PI * radius2. Since there are 2 Circles in the Cylinder, we multiply it by 2.

i.e., 2 (PI * radius2)

Next lets cut the cylinder at one end and open it. It forms a rectangle. Now height of the rectangle and height of the Cylinder are same. Width of the rectangle is equal to circumference of the circle, since top and bottom of the Cylinder are circles.

So, area of rectangle = (2 * PI * radius) * height

Therefore, Surface Area of Cylinder is:

SA = ( (2 * PI * radius) * height ) + ( 2 (PI * radius2) );

Expected Output for the Input

User Input:
Enter Radius and Height of the Cylinder
2
5

Output:
Surface Area of Cylinder is 87.920006

Video Tutorial: C Program To Calculate Surface Area of Cylinder


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

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

Source Code: C Program To Calculate Surface Area of Cylinder

#include<stdio.h>

int main()
{
    const float PI = 3.14;
          float r, h, Area;

    printf("Enter Radius and Height of the Cylinder\n");
    scanf("%f%f", &r, &h);

    Area = (2 * PI * r * h) + (2 * PI * r * r);

    printf("Surface Area of Cylinder is %f\n", Area);

    return 0;
}

Output 1:
Enter Radius and Height of the Cylinder
5
10
Surface Area of Cylinder is 471.000031

Output 2:
Enter Radius and Height of the Cylinder
2
13
Surface Area of Cylinder is 188.400009

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 Circumference of Circle

Given the radius of a Circle, write a C program to calculate circumference of the Circle.

Formula To Calculate Circumference of Circle

circumference = 2 * PI * radius;
Where PI is approximately equal to 3.14

Related Read:
Basic Arithmetic Operations In C

Expected Output for the Input

User Input:
Enter Radius of the Circle
5

Output:
Circumference of the Circle is 31.400002

Video Tutorial: C Program To Calculate Circumference of Circle


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

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

Source Code: C Program To Calculate Circumference of Circle

#include<stdio.h>

int main()
{
    const float PI = 3.14;
          float r, c;

    printf("Enter Radius of the Circle\n");
    scanf("%f", &r);

    c = 2 * PI * r;

    printf("Circumference of the Circle is %f\n", c);

    return 0;
}

Output 1:
Enter Radius of the Circle
10
Circumference of the Circle is 62.800003

Output 2:
Enter Radius of the Circle
41
Circumference of the Circle is 257.480011

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 Check If Point Lies Inside, Outside or On The Circle

Given the coordinates(cx, cy) of center of a circle and its radius, write a C program that will determine whether a point(x, y) lies inside the Circle, on the Circle or outside the Circle. (Hint: Use sqrt() and pow() functions)

Note:
Center Point – (cx, cy);
We need to find the position of point (x, y);

Logic To Check whether Point Lies Inside, Outside or On The Circle

First we need to calculate the distance of the point(x, y) from the center(cx, cy) of the circle. Next we need to compare the distance with the radius of the Circle.

Conditions To Determine The Position of the Point(x, y)
1. Distance is greater than radius: point is outside the Circle.
2. Distance is less than radius : point is inside the Circle.
3. Distance is equal to the radius: point is on the Circle.

Related Read:
Basic Arithmetic Operations In C
Calculate Power of a Number using pow(): C Program

Expected Output for the Input

User Input:
Enter the center point(cx, cy)
0
0
Enter radius of the circle
6
Enter the point(x, y) to check its position
3
3

Output:
Point (3.00, 3.00) is inside the Circle

Formula To Calculate Distance from point(x, y) To Center Point (cx, cy)

distance = sqrt( pow( (x – cx), 2 ) + pow( (y – cy), 2) );

Note: sqrt() and pow() are builtin method present in library file or header file math.h

Video Tutorial: C Program To Check If Point Lies Inside, Outside or On The Circle


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

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

Source Code: C Program To Check If Point Lies Inside, Outside or On The Circle

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

int main()
{
    float cx, cy, radius, x, y, distance;

    printf("Enter the center point(cx, cy)\n");
    scanf("%f%f", &cx, &cy);

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

    printf("Enter the point(x, y) to check its position\n");
    scanf("%f%f", &x, &y);

    distance = sqrt( pow( (x - cx), 2 ) + pow( (y - cy), 2 ) );

    if(distance < radius)
    {
        printf("Point (%0.2f, %0.2f) is inside the Circle\n", x, y);
    }
    else if(distance > radius)
    {
        printf("Point (%0.2f, %0.2f) is outside the Circle\n", x, y);
    }
    else
    {
        printf("Point (%0.2f, %0.2f) is on the Circle\n", x, y);
    }

    return 0;
}

Output 1:
Enter the center point(cx, cy)
0
0
Enter radius of the circle
6
Enter the point(x, y) to check its position
2
2
Point (2.00, 2.00) is inside the Circle

Output 2:
Enter the center point(cx, cy)
0
0
Enter radius of the circle
6
Enter the point(x, y) to check its position
12
6
Point (12.00, 6.00) is outside the Circle

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