C++ Program To Calculate Square And Cube Of Given Number Using Nested Macro Substitution

This Program is to find the square and cube of given number using nested macro substitution which is on of the easier way to find because it is declared at the #define header which helps us further in the program to use it directly to get the output. 

Before writing a program let us know what is macro substitution? and How it works?

Macro substitution

Macro substitution has a name and replacement text, defined with #define directive. The preprocessor simply replaces the name of a macro with replacement text from the place where the macro is defined in the source code.

Source Code

#include<iostream>
using namespace std;
#include<iomanip>
#define square(x)(x*x) //defining the square
#define cube(x)(square(x)*x) //defining the cube
int main()
{
int x;
cout<<"Enter the value for x :\n";
cin>>x;
cout<<"Square is "<<square(x)<<endl<<"cube is "<<cube(x)<<endl;    // Using the defined function to calculate square and cube
return 0;
}

OUTPUT


Enter the value for x :
5
Square is 25
cube is 125
Share To:

Arogya Thapa Magar

Post A Comment:

0 comments so far,add yours