Using Macro Template In Macro Expansion: C Program

In this video tutorial lets see how we can make use of a macro template in a macro expansion.

Objective

We define PI in a macro, and then we use PI in expansion of another macro.

We shall also modify the same program to make use of constant M_PI present in math.h library file instead of macro template PI.

Related Read:
Preprocessors In C Programming Language
C Program To Find Area of Circle Using Macro Expansion
Macros With Arguments: C Program

Video Tutorial: Using Macro Template In Macro Expansion: C Program


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

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

Source Code: Using Macro Template In Macro Expansion: C Program

#include<stdio.h>

#define PI 3.14159265358979323846

#define AREA(r) ( PI * r * r )

int main()
{
    float r;

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

    printf("Area of Circle is %f\n", AREA(r));

    return 0;
}

Output:
Enter radius of Circle
5
Area of Circle is 78.539816

In above source code, preprocessor replaces every occurrence of PI with 3.14159265358979323846 and then checks for every occurrence of AREA(r) and replaces with ( 3.14159265358979323846 * r * r ).

With this simple example I’m trying to illustrate that we can make use of Macro template inside macro expansion of another macro definition.

Using M_PI constant present in Math.h header file

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

#define AREA(r) ( M_PI * r * r )

int main()
{
    float r;

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

    printf("Area of Circle is %f\n", AREA(r));

    return 0;
}

Output:
Enter radius of Circle
5
Area of Circle is 78.539816

Note: M_PI is also a macro inside math.h library file.

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 Convert Radian To Degree

Lets write a C program to convert angle from Radian to Degree.

Formula To Convert Radian To Degree

degree = radian x (180.0 / M_PI);

where M_PI is a constant present in header file / library file math.h and its approximately equal to 3.14;

Related Read:
Keywords, Constants, Variables: C
Basic Arithmetic Operations In C
Print All Trigonometric Ratios: C Program
C Program To Convert Degree To Radian

Video Tutorial: C Program To Convert Radian To Degree


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

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

Source Code: C Program To Convert Radian To Degree

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

int main()
{
    float radian, degree;

    printf("Enter angle in Radian\n");
    scanf("%f", &radian);

    degree = radian * ( 180.0 / M_PI );

    printf("Angle in Degree is %f\n", degree);

    return 0;
}

Output 1:
Enter angle in Radian
1
Angle in Degree is 57.295780

Output 2:
Enter angle in Radian
5
Angle in Degree is 286.478912

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 Convert Degree To Radian

Lets write a C program to convert angle from Degree to Radian.

Formula To Convert Degree To Radian

radian = degree x (M_PI / 180.0);

where M_PI is a constant present in header file / library file math.h and its approximately equal to 3.14;

Note: Trigonometric Ratios like Sin, Cos, Tan etc take radian value as input and not degrees. So its important to learn how to convert angles from degree to radian.

Related Read:
Keywords, Constants, Variables: C
Basic Arithmetic Operations In C
Print All Trigonometric Ratios: C Program
C Program To Convert Radian To Degree

Video Tutorial: C Program To Convert Degree To Radian


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

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

Source Code: C Program To Convert Degree To Radian

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

int main()
{
    float degree, radian;

    printf("Enter angle in Degrees\n");
    scanf("%f", &degree);

    radian = degree * ( M_PI / 180.0 );

    printf("Angle in Radian is %f\n", radian);

    return 0;
}

Output 1:
Enter angle in Degrees
90
Angle in Radian is 1.570796

Output 2:
Enter angle in Degrees
360
Angle in Radian is 6.283185

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 Sum of Square of Sine and Cosine of an Angle is 1

Write a C program to receive value of an angle in degrees and check whether sum of squares of sine and cosine of this angle is equal to 1.

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

Formula To Convert Angle From Degree To Radian

radian = degree x ( PI / 180.0)
where PI is 3.14159265359

Note: In C programming Language, we’ve a builtin constant M_PI which has value of PI.

Expected Output for the Input

User Input:
Enter angle in Degree
45

Output:
Sum of square of sin(45.00) and cos(45.00) is 1

Logic To Check If Sum of Square of Sine and Cosine of an Angle is 1

User enters the angle in degree. We convert angle from degree to radian.
radian = degree x ( PI / 180.0)
Next we use pow() method present in math.h library to sqaure the values of sin(angle) and cos(angle). We add the square of sin(angle) and cos(angle) and store it inside the variable sum, and display appropriate message to the user.

Video Tutorial: C Program To Check If Sum of Square of Sine and Cosine of an Angle is 1


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

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

Source Code: C Program To Check If Sum of Square of Sine and Cosine of an Angle is 1

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

int main()
{
    float sum = 0, angle, temp;

    printf("Enter angle in Degree\n");
    scanf("%f", &angle);

    temp   = angle;

    angle  = angle * ( M_PI / 180.0 );

    sum    = (  pow(sin(angle), 2) + pow(cos(angle), 2)  );

    if(sum == 1)
    {
        printf("Sum of square of sin(%0.2f) and cos(%0.2f) is 1\n", temp, temp);
    }
    else
    {
        printf("Sum of square of sin(%0.2f) and cos(%0.2f) is not 1\n", temp, temp);
    }

    return 0;
}

Output 1:
Enter angle in Degree
30
Sum of square of sin(30.00) and cos(30.00) is 1

Output 2:
Enter angle in Degree
45
Sum of square of sin(45.00) and cos(45.00) is 1

Output 3:
Enter angle in Degree
60
Sum of square of sin(60.00) and cos(60.00) is 1

Output 4:
Enter angle in Degree
75
Sum of square of sin(75.00) and cos(75.00) is 1

Output 5:
Enter angle in Degree
90
Sum of square of sin(90.00) and cos(90.00) is 1

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

Calculate Area of a Circle using math.h library: C

We can calculate area of circle if we know the value of its radius. So using that, we shall write the logic in c program to calculate the area of a circle, and we will us math.h library functions in this program.

Related Read:
Basic Arithmetic Operations In C

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

int main()
{
    float radius;

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

    printf("Area of circle is %f \n", (M_PI * pow(radius, 2)));

    return 0;
}

Output:
Enter radius of circle
2.0
Area of circle is 12.566371

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

Calculate Area of a Circle using math.h library: C


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

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


math.h library file has a constant M_PI which has the value of PI. Since it’s a constant variable we can not change its value in our program. We’re also using pow() method/function present in math.h header file.

Formula for calculating Area of a circle


area of circle

Formula for calculating Area of a circle is PI * radius * radius. We take value of radius from user and using above formula calculate area of the circle and output result to the console window. Since we’re using math.h library file we use M_PI and pow() methods for the expression.

i.e., area = M_PI * pow(radius, 2);

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