By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,924 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,924 IT Pros & Developers. It's quick & easy.

how to write code for X power N with out using POW function.

P: 1
Dear members can u please tell me how to write code for x power n with out using pow() function.The manipulation code must be in only one line.
Dec 18 '06 #1
Share this Question
Share on Google+
2 Replies


P: 10
Dear members can u please tell me how to write code for x power n with out using pow() function.The manipulation code must be in only one line.

Hi buddy,

It very easy use pow() function.

Include a header file math.h and then add this function

z = pow( x, n );

That's it! it ll work fine..
Dec 18 '06 #2

P: 40
Dear members can u please tell me how to write code for x power n with out using pow() function.The manipulation code must be in only one line.

Hello it's very easy... Just think on it and try.. If then too u did'nt get then look on this following solution...

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int pw(int,int);
  3. int main()
  4. {
  5. int x,n,ans;
  6. printf("Enter the numbers:base and power\n");
  7. scanf("%d%d",&x,&n);
  8. ans=pw(x,n);
  9. printf("The ans is:  %d\n",ans);
  10. return 0;
  11. }
  12.  
  13.  
  14. int pw(int x,int n)
  15. {
  16. int i,y=1;
  17. if(n==0)
  18. return 0;
  19. else if(n==1)
  20. return x;
  21. else if(n==2)
  22. return(x*x);
  23. else
  24. {
  25. y=x*x;
  26. for(i=3; i<=n; i++)
  27. {
  28. y=y*x;
  29. }
  30. return (y);
  31. }
  32. }
Dec 18 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.