ccplusplus.com
Learn C, C++ Concepts
Monday, September 19, 2011
c program to calculate power of a number
/************************************************************* * File : calculating-power-of-number.c * Author : Saurabh Gupta * Desc : power of a number in c * Source : http://saurabhgupta0527.blogspot.com/p/c.html * Created : AM 06:21 19 September 2011 *************************************************************/ #include <stdio.h> #include <math.h> int main() { printf ("\n"); printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); printf ("XXXXX calculating power of a number XXXXX\n"); printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); double power(double,int); double n,ans; int p; printf(""); printf("\nEnter the base number = "); scanf("%lf",&n); printf("\nEnter the power = "); scanf("%d",&p); ans=power(n,p); printf("\nResult of %.2lf to power %d is %.2lf",n,p,ans); printf ("\n"); printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); return 0; } double power(double nn,int pp) { if(pp==0) { return (1); } else { return(nn*power(nn,pp-1)); } } /* * OUTPUT * [sgupta@rhel55x86 c]$ gcc calculating-power-of-number.c -o calculating-power-of-number [sgupta@rhel55x86 c]$ ./calculating-power-of-number XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXX calculating power of a number XXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Enter the base number = 5 Enter the power = 2 Result of 5.00 to power 2 is 25.00 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX [sgupta@rhel55x86 c]$ */ <textarea>
See also
Other popular tricky C Sample Codes and language Concept
.
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment