Tuesday, April 23, 2024
HomeC ProgrammingC program to seek out sq. and dice of a quantity utilizing...

C program to seek out sq. and dice of a quantity utilizing macro – #outline SQUARE(x), #outline CUBE(x)


Write a C program to seek out sq. and dice of a quantity utilizing macro. The way to discover dice of a quantity utilizing macro #outline preprocessor directive in C program. Logic to seek out sq. and dice of a quantity utilizing macro.

Until now we now have coated fundamentals of macro find out how to outline, undefine and redefine a macro in C programming. On this put up I’ll clarify find out how to discover sq. and sum of two numbers utilizing macro,  #outline preprocessor directive in C program.

Required data

Primary C programming, Macros

The way to discover sq. and dice of a quantity utilizing macros?

In earlier put up we realized how environment friendly macros are at remodeling small capabilities with easy logic. We realized to create our personal macro to calculate sum of two numbers.

We’re already conscious with macro definition syntax, if not I’ve added it under. So, allow us to outline two macro which settle for a argument and return sq. and dice of given quantity. 

Syntax:

#outline MACRO_NAME(params) MACRO_BODY

The place MACRO_NAME is title of the macro. params is parameters handed to macro. MACRO_BODY is the physique the place we are going to write precise logic of macro.

Instance:

#outline SQUARE(x) (x * x)
#outline CUBE(x) (x * x * x)

Program to seek out sq. and dice of a quantity utilizing macro

/**
 * C program to seek out sq. and dice of a quantity utilizing macro
 */

#embrace <stdio.h>

// Outline macro to seek out sq. and dice
#outline SQUARE(x) (x * x)
#outline CUBE(x) (x * x * x)

int fundamental()
{
    int num;

    // Enter a quantity from consumer
    printf("Enter any quantity to seek out sq. and dice: ");
    scanf("%d", &num);

    // Calculate and print sq.
    printf("SQUARE(%d) = %dn", num, SQUARE(num));

    // Calculate and print dice
    printf("CUBE(%d) = %dn", num, CUBE(num));

    return 0;
}
Enter any quantity to seek out sq. and dice: 10
SQUARE(10) = 100
CUBE(10) = 1000

Completely satisfied coding 😉

About Pankaj

Pankaj Prakash is the founder, editor and blogger at Codeforwin. He likes to be taught new techs and write programming articles particularly for freshmen. He works at Vasudhaika Software program Sols. as a Software program Design Engineer and manages Codeforwin. In brief Pankaj is Net developer, Blogger, Learner, Tech and Music lover.

Comply with on: Twitter | Google | Web site or



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments