Monday, 23 February 2015

U3P9

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 the square whenthis argument is omitted. Write a main that gets the value of m and n from theuser to test the function.

 #include<iostream.h>  
 #include<conio.h>  
 #include<math.h>  
 double power(double m, int n)  
 {  
      double ans = pow(m,n);  
      return ans;  
 }  
 void main()  
 {  
      clrscr();  
      double a;  
      int b;  
      char ch;  
      cout<<"Enter the double value of base m : ";  
      cin>>a;  
      cout<<"\nDefault value of power is 2. Want to give different value?(Y/N) : ";  
      cin>>ch;  
      if(ch=='y' || ch=='Y')  
      {  
           cout<<"\nEnter integer value for power : ";  
           cin>>b;  
           cout<<"\nValue of Number "<<a<<" to the Power of "<<b<<" is "<<power(a,b);  
      }  
      else  
      {  
           cout<<"\nValue of Number "<<a<<" to the Power of 2 is "<<power(a,2);  
      }  
      getch();  
 }  

OUTPUT :

No comments:

Post a Comment