Use a macro to calculate the area and circumference of a circle.
#define, arithmetic operators, and macros.
A macro is a symbol defined by #define that replaces text before compilation. Here, we define PI as 3.14159 and use it to calculate:
PI * r * r2 * PI * rThis makes the code easier to read and avoids repeating the same constant many times.
#include <stdio.h>
#define PI 3.14159
int main() {
float r, area, circumference;
printf("Enter the radius: ");
scanf("%f", &r);
area = PI * r * r;
circumference = 2 * PI * r;
printf("Area = %.2f\n", area);
printf("Circumference = %.2f\n", circumference);
return 0;
}
Input: radius = 5
Output: Area = 78.54
Circumference = 31.42
Use macros to calculate the square and cube of a number.
Macros and arithmetic operators.
We define two macros:
SQUARE(x) = x * xCUBE(x) = x * x * xThen we call these macros with a value and print the results. This demonstrates how macros help simplify repeated computations.
#include <stdio.h>
#define SQUARE(x) ((x) * (x))
#define CUBE(x) ((x) * (x) * (x))
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Square = %d\n", SQUARE(n));
printf("Cube = %d\n", CUBE(n));
return 0;
}
Input: 3
Output: Square = 9
Cube = 27
%Check whether a number is even or odd using the remainder operator.
Remainder and comparison operators.
The % operator gives the remainder after division. When a number is divided by 2:
This is a common way to test parity in C.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("Even\n");
} else {
printf("Odd\n");
}
return 0;
}
Input: 10
Output: Even
Swap two numbers without using a temporary variable.
Assignment and arithmetic or bitwise operators.
To swap two numbers without a temporary variable, we can use arithmetic:
a = a + bb = a - ba = a - bAfter these steps, the values are exchanged. This works because the original values are used in the calculation.
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
a = a + b;
b = a - b;
a = a - b;
printf("After swapping: %d %d\n", a, b);
return 0;
}
Input: 2 9
Output: After swapping: 9 2