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

Sunday, April 4th, 2010

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();
}
Avatar of Ranjith

Author Name :
Ranjith

Total : 7 Comments


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

  1. dynamoblaze says:

    Sir, this gives a^b+1

  2. swati parmar says:

    #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. Rahul says:

    #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. Mani says:

    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. Mani says:

    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 says:

    thanks

  7. Priyam Gaurav says:

    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.

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup

Email: