Preprocessor Directives in C++ Part – 2

Master C++ with Real-time Projects and Kickstart Your Career Start Now!!

Program 1

// Preprocessor 
#include <iostream> 
using namespace std;
#define factorial(n){\
    int f=1;\
    while(n!=0)\
    {\
           f=f*n;\
           n--;\
    }\
    cout<<"factorial is : "<<f;\
}
 int main()     
 {
    system("cls");
    int n;
    cout<<"Enter a number: ";
    cin>>n;
    factorial(n);
    return 0;
 }

// #define sum(n){\
//     int i=1, s=0; \
//     while(i <= n) { \
//         s = s + i; \
//         i++; \
//     } \
//     cout<<"sum is: "<<s;\
// }
// int main()     
// {
//     system("cls");
//     int l;
//     cout<<"Enter a limit :";
//     cin>>l;
//      sum(l)
//     return 0;
// }

Program 2

#include <iostream> 
using namespace std;
#define add(a,b) a+b
int main()
{
    system("cls");
    int a,b,c;
    cout<<"Enter two number : ";
    cin>>a>>b;
    #ifdef add(a,b)
        c=add(a,b);
        cout<<"Macro";
     #else
       c=a+b;
       cout<<"addition";
     #endif
     cout<<"Addition is : "<<c;     
     #undef add(a,b)
     c=add(a,b);
     cout<<c;
    return 0;
}

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

courses
Image

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *