Find out Power without using POW function in C
Introduction: In this discussion, we will discuss how we find out power without using the POW function in C. In this article, you'll understand how to compute integer powers in C without using the pow() function, with programmatic help.
Use a for loop to determine the power of a given integer:
Let, we need to find P^Q. The steps are as follows –
- Let the input be P^Q. P is the base, and Q is the exponent.
- Initialize Power = 1.
- Repeat the following steps Q times in a loop.
Power = Power * P 4. - The final answer P^Q is stored in Power.
Example 1: Here, we give an example of how to find out power without using the POW function in C. The example is given below –
# include < stdio. h >
# include < string. h>
# include < conio. h>
# include < math. h >
# include < stdlib. h>
int main ()
{
int n, exp1, exp2;
long long int value = 1;
printf ( " Enter the number and also its exponential : \ n \ n " ) ;
scanf ( " % d % d " , & n , & exp1 ) ;
exp2 = exp1;
while ( exp1 - - > 0 )
{
value * = n;
}
printf ( " \ n \ n % d ^ % d = % l l d \ n \ n " , n , exp2 , value ) ;
return 0;
}
Output: We compile the above programs and also run the above program. After compilation, the result is –
Enter the number and also it is exponential:
6
5
6 ^ 4 = 1296
Example 2: Here, we give another example of how to find out power without using the POW function in C. The example is given below –
#include <stdio.h>
#include <string.h>
# include < conio. h>
# include < math. h >
int pow (int X, int Y)
{
int power = 1, i;
for (i = 1; i <= Y; ++i)
{
power = power * X;
}
return power;
}
int main ()
{
long long int base, exponent;
printf ("Enter Base value: ");
scanf ("%d", &base);
printf ("Enter Power value: ");
scanf ("%d", &exponent);
printf ("%d ^ %d = %d", base, exponent, Pow (base, exponent));
return 0;
}
Output: We compile the above programs and also run the above program. After compilation the result is –
Enter Base value: 3
Enter Power value: 5
3 ^ 5 = 243
Example 3: Here we give another example of how to find out power without using POW function in C. The example is given below –
# include < stdio. h >
# include < string. h >
# include < conio. h >
# include < math. h >
# include < stdlib. h >
int Pow (int x, int y)
{
if (y = = 0)
return 1;
else
return Pow (x, y - 1) * X;
}
int main ()
{
long long int base, exponent;
printf ("Enter the Base value: ");
scanf (" % d ", & base);
printf ("Enter the Power value: ");
scanf (" % d ", & exponent);
printf (" % d ^ % d = % d ", base, exponent, Pow (base, exponent));
return 0;
}
Output: We compile the above programs and also run the above program. After compilation the result is –
Enter the Base value: 5
Enter the Power value: 5
5 ^ 5 = 3125
Example 4: Here we give another example of how to find out power without using POW function in C. The example is given below –
#include <stdio.h>
#include <string.h>
int Pow (int X, int Y)
{
if (Y == 0)
return 1;
else
return Pow (X, Y-1) * X;
}
int main ()
{
long long int base, exponent;
printf ("Enter the Base value: ");
scanf ("%d", &base);
printf ("Enter the Power value: ");
scanf ("%d", &exponent);
printf ("%d ^ %d = %d", base, exponent, Pow (base, exponent));
return 0;
}
Output: We compile the above programs and also run the above program. After compilation the result is –
Enter the Base value: 5
Enter the Power value: 4
5 ^ 5 = 625
So, in this article we are discussing about how we find out power without using the POW function in C. We give some examples of pow() function and also share the output.