Write a C program to search out sq. and dice of a quantity utilizing macro. Methods to discover dice of a quantity utilizing macro #outline
preprocessor directive in C program. Logic to search out sq. and dice of a quantity utilizing macro.
Until now we’ve coated fundamentals of macro easy methods to outline, undefine and redefine a macro in C programming. On this put up I’ll clarify easy methods to discover sq. and sum of two numbers utilizing macro, #outline
preprocessor directive in C program.
Required information
Learn extra easy methods to discover dice of a quantity utilizing features?
Methods to discover sq. and dice of a quantity utilizing macros?
In earlier put up we discovered how environment friendly macros are at remodeling small features with easy logic. We discovered 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 easy methods 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’ll write precise logic of macro.
Instance:
#outline SQUARE(x) (x * x)
#outline CUBE(x) (x * x * x)
Program to search out sq. and dice of a quantity utilizing macro
/**
* C program to search out sq. and dice of a quantity utilizing macro
*/
#embrace <stdio.h>
// Outline macro to search out sq. and dice
#outline SQUARE(x) (x * x)
#outline CUBE(x) (x * x * x)
int major()
{
int num;
// Enter a quantity from person
printf("Enter any quantity to search 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 search out sq. and dice: 10 SQUARE(10) = 100 CUBE(10) = 1000
Joyful coding 😉