In this post I am going to solve one problem power of a number using pow function.
So,
How to solve this problem?
Here user first input two numbers and find there power using pow() function. So, here how to find power of two number in C Programming. How to use pow() function in C Programming.
Example:
Input
Enter base: 5
Enter exponent: 2
Output
5 ^ 2 = 25
Program to Find power of a number:
/** * C program to find power of any number */ #include <stdio.h> #include <math.h> // Used for pow() function int main() { double base, expo, power; /* Input two numbers from user */ printf("Enter base: "); scanf("%lf", &base); printf("Enter exponent: "); scanf("%lf", &expo); /* Calculates base^expo */ power = pow(base, expo); printf("%.2lf ^ %.2lf = %.2lf", base, expo, power); return 0; }
Output:
Enter base: 5
Enter exponent: 3
5 ^ 3 = 125
Here %.2lf is used to to print fractional value up to 2 decimal places.
Step-1:
line no. -1 to 3 is a comment to "find power of any number"
Step-2:
line no. 5 and 6 used header files "<stdio.h> <math.h>" line no. 5 for scanf() and printf() and line no. 6 used for pow() "<math.h>"
Step-3:
line no. 8 "int main()" execution begins here.
Step-4:
line no. 13 and 14 used for print on console (printf("Enter base: ");) and take input from user(scanf("%lf", &base);)
Step-5:
line no. 15 and 16 used for input expo by help of [
printf("Enter exponent: "); => used for console before take input
scanf("%lf", &expo); ]
Step-6:
line no 18 used for comment and 19 used for solving the problem by expression [/* Calculates base^expo */
power = pow(base, expo);
]
Step-7:
line no 21 print by help of printf()
[printf("%.2lf ^ %.2lf = %.2lf", base, expo, power);] here we used %.2lf for fractional value up to 2 decimal places.
Step-8:
return 0; //successful execution by return to main()
Please comment and share this post if you find valuable information and wants to contribute please WhatsApp us.