Scientific Notation in C
In this c program, we will learn how to print float or double value in exponential (scientific) format in c programming language?
Which format specifier specifies exponential format?
%e can be used to print value in exponential format of float or double value.
Let’s consider the following example - in this example there is a float type variable value and which is assigned by 123456.456 and value of variable value is printing using %f and %e format specifiers.
C program - Print value in Exponential Format
/*C program to print value in exponential (scientific) format */ #include <stdio.h> int main() { float value=123456.456f; printf("value: %f (using %%f format specifier)\n",value); printf("value: %e (using %%e format specifier)\n",value); return 0; }
value: 123456.453125 (using %f format specifier)
value: 1.234565e+05 (using %e format specifier)
How to represent scientific notation in C
How do I represent extremely large or small numbers in C with a certain amount of significant figures. For example, if I want to do calculations on 1.54334E-34, how could I do this. Also, is this applicable to C language code?
#include <stdio.h>
int main()
{
float var = 1.54334E-34;
double var2 = 1.54334E-34;
printf("\n normal:%f\n sci:%e \n or \n sci:%E \n",var,var,var);
printf("\n normal:%f\n sci:%e \n or \n sci:%E \n",var2,var2* 1.0E3 ,var2 * 1.0e3);
return 0;
}
Output:
sci:1.543340e-34
or
sci:1.543340E-34
normal:0.000000
sci:1.543340e-31
or
sci:1.543340E-31
#include <stdio.h> int main() { float lightyear = 5.878E12; float jupiter = 483400000; float distance; distance = jupiter/lightyear; printf("Jupiter is %E light years from the sun.\n",distance); return(0); }
Jupiter is 8.223886E-005 light years from the sun.

No comments:
Post a Comment