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();
}
Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

15 thoughts on “C++ program to calculate a to the power b without using power function

  1. 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;
    }

  2. 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;
    }

  3. 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.

  4. #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;
    }

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

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

  7. #include
    #include
    using namespace std;

    int main() {
    int exp;
    float base;

    cout <> base >> exp;

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

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in