×

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 –

  1. Let the input be P^Q. P is the base, and Q is the exponent.
  2. Initialize Power = 1.
  3. Repeat the following steps Q times in a loop.
    Power = Power * P 4.
  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.


Related Topics

Stdio.h in C

Header files are used to make the programmer’s efforts a lot easier. In order to make the programming simple, there are a number of libraries which are included as predefined...

4 minutes read.

pow() function in C

pow() function is one of the in-built functions present in math.h header file Power function is used to calculate the powers of the given number. The syntax of the power...

3 minutes read.

Reverse a Stack using Recursion in C

In this tutorial we will learn how recursion will be used in this case to reverse a stack. For loops, while loops, do-while loops, and similar constructions are not permitted....

5 minutes read.

Sum of digits in C++

Sum of digits in C++ There are various ways to find the sum of the digits of a number in C++. We can use containers like arrays or other simple cases...

4 minutes read.

Functions in C

What is function? Functions are defined as the set of announcements which take inputs, perform operations and provide results. The operation of a function only performs when the function gets called....

4 minutes read.

Armstrong Number in C

The Armstrong number is defined as the sum of each of its digits to the power of the number base for the each given number with any given number base....

3 minutes read.

C/C++ Program to Find the Size of int, float, double and char

In this tutorial, we will learn how to use the sizeof operator to determine the size of each variable. Program to Determine Variable Size Write a C or C++ program to determine the...

2 minutes read.

getc() function in C

Getc is one of the file handling technique in C. The Getc() is a C library function gets the next character or new characters  from the specific stream and supports...

4 minutes read.

Errors in C

Errors in C Errors are nothing but problems or faults that pretty much occur in all the programming languages. Errors make the behavior of the program seem abnormal, and even the...

4 minutes read.

Self-referential structure in C

Self-referential structure in C A self-referential structure is a structure that can have members which point to a structure variable of the same type. They can have one or more pointers...

3 minutes read.

Continue in C

C language: C language is a procedure oriented programming language. We can say that it is a platform dependent language. C language is introduced by Dennis Ritchie in the year 1970. We...

2 minutes read.

10 Best IDEs for C or C++ Developers in 2024

Nobody can deny the fact that C and C++ were the first programming languages used by significant developers worldwide. Even now, newcomers who want to start programming are most frequently...

6 minutes read.

Enumeration (Enum) in C

Enumeration (Enum) in C: In the C programming language, the enum is referred to as an enumerated type. Enum is a user defined data type consisting of integer values and...

3 minutes read.

C Program to find the size of a File

Before knowing about the Program, you need to understand what the size of a file is. After that, you should know how it is going to work with the given...

4 minutes read.

Advantages of Dynamic Memory Allocation in C

Dynamic Memory Allocation Dynamic memory allocation is a process of assigning or allocating memory to a variable during the execution of a program. Dynamic memory allocation provides storage according to the...

3 minutes read.

How to use atoi() function in C

Introduction: The atoi() is a function used in C programming language. This function converts string characters to the integer value. The atoi() function converts the input string to the return type of...

4 minutes read.

Bar3d() function in C Graphics

The bar3d function is used to create a 2-dimensional filled-in rectangular bar. We can also create three-dimensional shapes in C using helpful function. The first step in creating this 3D...

3 minutes read.

While Loop Syntax in C

What is Loop? The statements in the sequence are repeatedly executed via looping statements in C until the condition is met. The body of a loop and a control statement make...

4 minutes read.

memmove() in C

Introduction: In this article we are discuss about the memmove() function in C. This function transfers memory blocks from one location to another. The memmove() function is declared in the...

3 minutes read.

Else If Ladder in C

What is Else If Ladder: When there are multiple options and a user has to decide from the options, we use Else If Ladder. Basically, it is an extension of if...

3 minutes read.