numbers - How to store pow(10,n) integer values in c where n can be <200? -


#include<stdio.h>    #include<math.h>       int main(void) {     long int n;     scanf("%d",&n);     n=pow(10,n);     printf("%ld\n",n);     solve(n);     return 0; } 

you need use double, not long (since 1080 won't fit in 64 bits long can express numbers below 9223372036854775807). read the floating point guide, since difficult subject (notice floating point numbers not same mathematical real numbers).

you might try:

#include <stdio.h>    #include <stdlib.h> #include <math.h>       int main(void) {   int n = 0;   if (scanf("%d",&n)<1) { perror("scanf"); exit(exit_failure); };   if (n < -256 || n > 256)      { fprintf(stderr, "wrong exponent %d\n", n);       exit(exit_failure); };   double x = pow(10.0,(double)n);   printf("ten power %d %g\n", n, x);   return 0; } 

(i removed call solve did not define; tested success of scanf , range of n)

for larger exponents, you'll need bignums perhaps using gmplib.

don't forget enable warnings & debug info when compiling. if compiled original code gcc -wall -wextra -g you'll warned many times (nearly 1 warning per line!).


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -