Explanation
The program calculates and prints the power of two numbers entered by the user. Specifically, it computes \( x \) raised to the power \( y \).
Header
The code begins with including the standard input-output header files using #include <stdio.h>
and #include <math.h>
for mathematical functions.
Variable Declaration
The main()
function is defined, and two variables of type long double
are declared:
long double d
: Stores the base number.long double m
: Stores the exponent.
Program Description
The program prints a description message for the user to understand its functionality and gives an example of the operation.
User Input
The user is prompted to enter a base number and its exponent. The input is read using scanf("%Lf %Lf", &d, &m);
.
Power Calculation and Output
The program calculates the power using the pow
function from the math library and prints the result:
printf("\n Power for the entered number %.1Lf to the power %.1Lf is: %.1Lf", d, m, pow(d, m));
: Prints the base number, the exponent, and the result of raising the base to the power of the exponent.
Program End
The program ends with printf("\n");
to print a new line and return 0;
to indicate successful execution.
Code
/*
* -----------------------------------------------------------
* Logic Building with Computer Programming (CSU1128)
* Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
*
* This code is a part of the educational initiative by dmj.one
* with aim of empowering and inspiring learners in the field of
* Computer Science and Engineering through the respective courses.
*
* (c) 2022, Divya Mohan for dmj.one. All rights reserved.
* -----------------------------------------------------------
*/
#include <stdio.h>
#include <math.h>
int main()
{
long double d, m;
printf("\n\n Program to print the power of the two numbers entered by the user. (Example: 2 3) = 2 to the power 3 = 8\n\n");
printf("Enter a number and its power for which you want to print x raised to the power y (Example: 3 6) - and press enter: ");
scanf("%Lf %Lf", &d, &m);
printf("\n Power for the entered number %.1Lf to the power %.1Lf is: %.1Lf", d, m, pow(d, m));
printf("\n");
return 0;
}
Output
Program to print the power of the two numbers entered by the user. (Example: 2 3) = 2 to the power 3 = 8
Enter a number and its power for which you want to print x raised to the power y (Example: 3 6) -
and press enter: 3 7
Power for the entered number 3 to the power 7 is: 2187.0