Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » BeginnersRSS Feeds

Write a function power() to raise a number m to a power n

Posted By: Easy Tutor     Category: C++ Programming     Views: 65320

Write a function power() to raise a number m to a power n. The function takes a double value for m and int value for n and returns the result correctly. Use a default value of 2 for n to make the function to calculate squares when this argument is omitted. Write a main that gets the values of m and n from the user to test the function.

Code for Write a function power() to raise a number m to a power n in C++ Programming

/*www.DailyFreeCode.comDownload Projects, Sourcecodes, Tips and Tricks, Interview FAQs, Hotlinks and more....Logon to www.DailyFreeCode.com*//*Write a function power() to raise a number m to a power n.  Thefunction takes a double value for m and int value for n and returnsthe result correctly.  Use a default value of 2 for n to make thefunction to calculate squares when this argument is omitted.Write a main that gets the values of m and n from the user to testthe function.*/

#include <iostream.h>
#include <conio.h>
void main()
{
double power(double m,int n=2);
clrscr();
cout<<endl<<endl<<endl;
int choice,m,n;
double result;
do
{
cout<<"\n\n\nCHOICES\n\n";
cout<<"1)    Only Value of M\n";
cout<<"2)    Value for both M and N\n";
cout<<"3)    QUIT\n";
cout<<"ENTER YOUR CHOICE:-";
cin>>choice;
clrscr();
cout<<endl<<endl<<endl;
switch(choice)
{
    case 1 : cout<<"Enter Value for M:-";
               cin>>m;
            result=power(m);
            cout<<"Power function when default argument is used ="<<result;
            break;
   case 2 : cout<<"Enter Value for M and N:-";
               cin>>m>>n;
            result=power(m,n);
            cout<<"Power function when actual argument is use ="<<result;
            break;
   case 3 : gotoout;
   default: cout<<"Entered Value is Invalid, Try again";
}
}while(choice!=3);
out:
}


double power(double m,int n)
{
double pow=1,k=0;
for(int i=0;i<n;i++)
{
pow=pow*m;
}
return(pow);
}
  
Share: 


Didn't find what you were looking for? Find more on Write a function power() to raise a number m to a power n Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Write a function power() to raise a number m to a power n is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
Pruthvi Bharadwaj from United States Comment on: Sep 01
did not understand the function part double power(double m,int n)...i am a begginer...kindly help


View All Comments