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
Learn extra find out how to discover dice of a quantity utilizing capabilities?
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.
Learn extra find out how to discover energy of any quantity utilizing recursion?
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 😉