Friday, 22 June 2012

write a c++ program to define function power to raise a number m to a power n the function takes a double value for m and integer value for n and return the result correctly use a default value of 2 for n to make the function calculate squares when this argument is ommitted.

/*write a c++ program to define function power to raise a number m to a power n
the function takes a double value for m and integer value for n and return
the result correctly use a default value of 2  for n to make the function
calculate squares when this argument is ommitted.*/
#include<iostream.h>
#include<conio.h>

double power(double m, int n=2)
{
 double result=1.0;
 cout<<"\n Number = "<<m<<"\n Power = "<<n;
 for(int i=0; i<n; i++)
 {
  result=result*m;
 }
 return result;
}

void main()
{
 clrscr();
 double no,ans;
 int pow;
 char wish;
 cout<<"\n Enter A Number :- ";
 cin>>no;
 cout<<"\n You Wish To Enetr Power y/n = ";
 cin>>wish;
 if(wish=='y')
 {
  cout<<"\n Enetr A Power Please = ";
  cin>>pow;
  ans=power(no,pow);
   cout<<"\n Ans = "<<ans;
  }
 else
 {
   ans=power(no);
   cout<<"\n Ans = "<<ans;
 }
  getch();
}

/*
 Enter A Number :- 2.5

 You Wish To Enetr Power y/n = y

 Enetr A Power Please = 3

 Number = 2.5
 Power = 3
 Ans = 15.625
*/

No comments: