-->





Translate

power function c++ code using pointers

1 comment
Write a program in c++ programming that calculates the power of a number using pointers

User will input two numbers and program will calculate power. First number will be base and second number will be the power.

This code explains the following C++ Concepts

  • Functions with the same arguments but different return data types
  • How to use pointers
  • Using pointers value to perform an arithmetic operation

program prototypes
power function pointers example c++
functions prototypes
Program has been tested on code blocks c++ complier.
see also: pointers multiplication example, pointers addition example code

program source code:


#include <iostream>
using namespace std;


int Power(int *, int *);
float Power1(int *, int *);
double Power2(int *, int *);

int main()

{
    int a,b;

 cout<<"Enter 1st Value:";
 cin>>a;

 cout<<"Enter 2nd Value:";
 cin>>b;

 cout<<endl <<endl;

 cout<<"...............Power.........." <<endl <<endl;

 cout<<"The Power of these numbers is:"<<Power(&a,&b)<<endl;
 cout<<"The Power of these numbers is:"<<Power1(&a,&b)<<endl;
 cout<<"The Power of these numbers is:"<<Power2(&a,&b)<<endl;

 cout<<endl <<endl;

 return 0;

}


int Power(int *x, int *y)

{
 int P=1;
 for(int i=0;i<*y;i++)
 {
  P=(P * *x);
 }
 return P;
}

float Power1(int *x, int *y)

{
 float P=1;
 for(int i=0;i<*y;i++)
 {
  P=(P * *x);
 }
 return P;
}

double Power2(int *x, int *y)

{
 double P=1;
 for(int i=0;i<*y;i++)
 {
   P=(P * *x);
 }
 return P;
}

program input-output
power function code example pointers c++

This program will help to clear the concept of functions with different return types. 

It shows the output of 3 functions having the same arguments but different return types.

Besides this, it shows how to use the value with pointers.
find more examples here: c++ examples code

1 comment: