logo codesdope
Level 1
1.
Write a macro to calculate area and perimeter of a rectangle.
Answer
#include<iostream>
using namespace std;
#define area(l,b) (l*b)
#define perimeter(l,b) (2*(l+b))
int main()
{
  cout << "Area is "<<area(5,2)<<" and perimeter is "<<perimeter(5,2)<<".\n";
  return 0;
}
                                   

2.
Write a macro to compare two numbers.
Answer
#include<iostream>
using namespace std;
#define equal(a,b) (a==b)
int main()
{
  cout << equal(5,2) << "\n";
  cout << equal(5,5) << "\n";
  return 0;
}                                  

3.
Write a macro to find average of two numbers.
Answer
#include<iostream>
using namespace std;
#define avg(a,b) ((a+b)/2.0)
int main()
{
  cout << avg(5,2) <<"\n";
  return 0;
}                                 

4.
Write a macro to find absolute value of number.
Answer
#include<iostream>
using namespace std;
#define abs(a) ((a<1)?(-1*a):a)
int main()
{
  cout << abs(-5) <<"\n";
  cout << abs(5) <<"\n";
  return 0;
}
                                  

5.
Write a macro to calculate simple interest from principal, rate of interest and time.
Simple interest = (principal*rate of interest*time)/100.
Answer
#include<iostream>
using namespace std;
#define si(p,r,t) ((p*r*t)/100.0)
int main()
{
  cout << si(1000,12,3) <<"\n";
  return 0;
}                                   
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.