C Program : print the power of a number using pow() function

Understanding the Pow() Function in C Programming

In the world of C programming, mathematical operations are crucial, and one function that stands out is pow(). This function allows you to raise a base number to an exponent, performing the operation of exponentiation. In this article, we'll delve into a simple C program that utilizes the pow() function to calculate the result and display it to the user.

The Code :

Let's take a closer look at the code snippet:


#include <stdio.h>
#include <math.h>
 main()
  {
    float a, b, c;
    printf(" Enter the values of b and c : ");
    scanf(" %f %f ", &b, &c);
    a = pow(b, c);
    printf("%f to the power of %f is %f ", b, c, a);
  }

Explanation

  1. Including Header Files:

    The program starts by including the necessary header files. stdio.h is included for input and output operations, and math.h is included for mathematical functions.

  2. Variable Declaration:

    Three float variables are declared - a, b, and c. These will be used to store the result and user-input values.

  3. User Input:

    The program prompts the user to enter the values of b and c. These values represent the base and exponent, respectively.

  4. Using the pow() Function:

    The pow() function is then employed to calculate b raised to the power of c. The result is stored in the variable a.

  5. Displaying Output:

    Finally, the program prints the result, showcasing the values of b, c, and the computed result using printf().

Input and Output Example:

Assuming the user enters the values 5 and 2 when prompted, the output would be:

  • Input: Enter the values of b and c: 5 2
  • Output: 5.0000 to the power of 2.0000 is 25.0000

This means that 5 raised to the power of 2 is indeed 25, as confirmed by the program's output.

In conclusion, the pow() function in C is a powerful tool for performing exponentiation, and this simple program demonstrates its usage in a real-world scenario. Feel free to explore further and experiment with different input values to understand how the pow() function behaves in various scenarios. Happy coding!

Post a Comment

0 Comments