In this article, we will print the power of any number without using pow() or an in-built function in C, C++, and Java programming languages.
pow() function is used to return the power of any number. pow() is an in-built function.
Example:
With pow() function
Input:
a = 2, b= 2
pow(2, 2);
Output:
4
Program to print the power of any number without the pow() function in C.
#include <stdio.h>
int main()
{
int a, b, i;
int power =1;
scanf("%d %d",&a,&b);
for(i=0; i<b; i++){
power = power*a;
}
printf("%d",power);
return 0;
}
Output:
2 3
8
Program to print the power of any number without the pow() function in C++.
#include <iostream>
using namespace std;
int main()
{
int a, b, i;
int power =1;
cin>> a>>b;
for(i=0; i<b; i++){
power = power*a;
}
cout<<power;
return 0;
}
Output:
2 3
8
Program to print the power of any number without the pow() function in Java.
public class Main
{
public static void main(String[] args) {
int a=2, b=3, i;
int power =1;
for(i=0; i<b; i++){
power = power*a;
}
System.out.println(power);
}
}
Output:
2 3
8
0 Comments