Implementation of square() Function
To implement a square()
function in C that calculates the square of a number, you can create a simple function that takes an input number and returns its square. Here's an example of how to implement the square()
function:
Example:
#include <stdio.h>
// Function to calculate the square of a number
double square(double num) {
return num * num;
}
int main() {
double number = 5.0;
double result = square(number);
printf("The square of %.2f is %.2f\n", number, result);
return 0;
}
In this example:
- The
square()
function takes a double-precision floating-point number (double num
) as its parameter and returns the square of that number by multiplying it by itself.
- In the
main()
function, a number (5.0
in this case) is passed to the square()
function, and the result is stored in the result
variable.
- Finally, the program prints the result, showing the square of the input number.
Output:
The square of 5.00 is 25.00
You can use this square()
function to calculate the square of any number by passing it as an argument to the function.