×

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

How to Calculate Time Complexity in C?

What is time complexity? An algorithm's time complexity measures how long it takes to complete a task in relation to the size of the input. It should be noted that the...

5 minutes read.

How to create a node in C?

A node is a small concept taken from the graph theory, or a node is a fundamental unit of a data structure, like a tree data structure. Every graph contains...

3 minutes read.

Sizeof in C

A pointer in C is defined as a variable that stores the information of the address of another variable. A pointer can be incremented or decremented, which means we can...

4 minutes read.

Binomial Coefficient Program in C

What is Binomial coefficient? In the given set of n possibilities, the binomial coefficient(n,k) indicates the order of choosing 'K' results from those possibilities. Binomial coeeficient of posistive n and k...

3 minutes read.

C vs Java Strings

String in C In C, we can define a string as a bunch of characters. A character array is distinguished from a string by the presence of the special character '\0'...

5 minutes read.

Goto and Labels in C

Introduction Goto in C: The goto statement is known as the jump statement in C. The goto is used to transfer the control of a program to a predefined label. The goto...

3 minutes read.

Purpose of a Function Prototype in C

A function prototype in C is a declaration of a function that specifies the function's name, return type and parameters. It has the following syntax: return_type function_name(parameter_list); For example, the prototype for...

4 minutes read.

Calendar application in C

Introduction to the calendar application We all are familiar with the calendar. It plays a crucial role in our daily life. We run toward the calendar whenever we want to know...

6 minutes read.

Data Structures And Algorithms in C

C language is one of the most flexible and simple languages. So, it is always better to learn any concept in C language compared to other languages. Let us look...

13 minutes read.

Actual and Formal Parameters

Any variable declared within the parenthesis is referred to as the parameters during the function declaration. Parameters tell the function about the argument datatype, their order, and the number of...

5 minutes read.

C Token

C Token and Keyword The C tokens are the basic buildings blocks in C language which are constructed together to write a C program. Each and every smallest individual unit in the C program is...

2 minutes read.

Projects on C language in 2024

Introduction Most aspiring programmers learn the C language as their first high-level programming language. The most flexible language utilized in every industry is, without a doubt, C. Even after 50 years...

9 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

C Switch Statements

Switch-case statement comes under the Selection control statement; there are four types of Control statements Decision-making statements (if, if-else)Selection statements (Switch-case)Iteration statements (for, while, do-while)Jump statements (break, continue, goto) If we want...

4 minutes read.

Difference between rand() and srand() function in C

What is the rand()? The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.

Variables in C

A variable can be defined as a name allocated to a storage space that can be manipulated by our programs. Every variable in C arbitrates about the overall layout and...

5 minutes read.

Armstrong program in C using function

In this article we will learn how to find Armstrong of a number using function in C. An Armstrong number is a three-digit number that is the sum of its separate...

1 minute read.

Selection sort in C

In the C standard, the selection sorting technique exists where the smallest among the unsorted elements of the array is selected at each time and is then inserted into its...

3 minutes read.

What are linker and loader in C

Linker and loader are utility programs that have a significant role in executing a program. Linker: A linker is a program that joins the object files produced by the assembler/ compiler...

3 minutes read.

Storage class in C

Storage class in C defines the scope, the visibility, and the lifetime of variables and functions. In other words, storage classes are used to describe the features of variables and...

3 minutes read.