# C++ program to calculate a to the power b without using power function

Write a C++ program to calculate a to the power b without using power function.

```#include<iostream.h> #include<conio.h> void main() { int a,b,i,temp=1; clrscr(); cout<<"Enter number for operation"; cin>>a>>b; for(i=1;i<=b;i++) { temp=temp*a; } cout<<endl<<"Result are:: "<<temp; getch(); }```

### 24 Responses to “C++ program to calculate a to the power b without using power function”

1. dynamoblaze

Sir, this gives a^b+1

• why did it not run?

2. swati parmar

#include
#include
void main()
{
int n,i,c;
clrscr();
printf(“\nEnter a number: “);
scanf(“%d”,&n);
for(i=1;i<=n ;i++)
{
c=n*n;
}
printf("\npower of a number is: %d",c);
getch();
}

3. #include
#include

class power
{
int base,exp,i,temp;
public:
void get();
void cal();
void dis();
power()
{
temp=1;
}
};

void power::get()
{
cout<>base>>exp;
}
void power::cal()
{
for(i=1;i<=exp;i++)
{
temp=temp*base;
}
}
void power::dis()
{
cout<<"Calculated="<<temp;
}
void main()
{
clrscr();
power p;
p.get();
p.cal();
p.dis();
getch();
}

4. Above program gives wrong answers as well as they occupy high memory any more time to type the simple program is
# include
void main()
{
int a;
cout<>a;
cout<<"\n power of "<<a<<" is "<<a*a;
}

5. SORRY GUYS I MISTAKED IN ABOVE PROGRAM THE CORRECT CODE IS HERE
#include
void main()
{
int a;
cin>>a;
cout<<"\n power of "<<a<<" is
"<<a*a;
}

6. aarthy ravichandran

thanks

7. Priyam Gaurav

This problem can be solved by applying different methods.
1st Method:

#include
#include
void main()
{
int a,b,c,i;
clrscr;
printf(“Enter the base: “);
scanf(“%d”,&a);
printf(“Enter the power: “);
scanf(“%d”,&b);
c=a;
for(i=1;i<b;i++)
{
c=c*a;
}
printf("Result = %d",c);
getch();
}

2nd Method:

#include
#include
void power(int a, int b);
void main()
{
int a,b;
clrscr();
printf(“Enter the base: “);
scanf(“%d”,&a);
printf(“Enter the power: “);
scanf(“%d”,&b);
power(a,b);
getch();
}
void power(int a, int b)
{
int c;
c=a;
for(i=1;i<b;i++)
{
c=c*a;
}
printf("Result = %d",c);
}

There are so many different methods to solve this problem.

8. #include
#include

int main ()
{
int a,b,i,temp=1;

printf (“enter value :”);
scanf (“%d”,&a);
printf ( “enter power value :”);
scanf (“%d”,&b);
for (i=1;i<=b;i++)
{
temp= a*temp;
}
printf("vlaue is :%d", temp);

getch();
return 0;
}

9. bilal iplanetz.com 03335106003

this is easiest one take copy pencil insert value in loop and see how ezily it will

10. Thanx Guys.
U r doing great.

11. navin kumar

how to find power of number without using pow function and multiplication operator ?

• #include
#include
using namespace std;

int main() {
int exp;
float base;

cout <> base >> exp;

cout << "Result = " << pow(base, exp);
return 0;
}

12. Write a program to find power of a number ( exponent value should be negative or 0.)

13. Mayank Pathania

int main ()
{ int b, a;
for ( int n=1; n<=a; n++)
{b=a*n;
}
count<<b;}

14. i want c++ program for triple power? send immediately …………………….

15. dhanashri

write the program accept two number a&b and calculate a*b,a/b,a%b. without using *,/,% operators ..

• #include
#include
void main()
{
printf(“enter the value of a & b\n”);
scanf(“%f%f”,&a,&b);
sub=a-b;
mul=a*b;
div=a%b;
printf(“the value of addition is = %f\n”,add);
printf(“the value of subtract is = %f\n”,sub);
printf(“the value of multiplication is = %f\n”,mul);
printf(“the value of divide is = %f\n”,div);
getch();
}

16. Manohar K.

Thank You Very Much…

17. Shekhar K. Aashutosh

Thank You Very Much…This Helped me a lot…

18. sir plz give me the solution of a c program :
write a program of a ‘C’ program to find out the value of y in the following eq.
y=x*x+3x+1

19. sir write a program in c++ to calculate (a^b) without using any built in fuction (please code)

20. How can i calculate power of any number or value without using loop,lib function,or recursion…..Have you any idea about??

##### More from Basic Programs
###### Java program to find whether no. is palindrome or not ### Research Papers ### Projects 