Wednesday 16 November 2011

How do I print a number in scientific notation? in C programming

How do I print a number in scientific notation?

To print a number in scientific notation, you must use the %e format specifier with the printf function, like so:

float f = 123456.78;
printf( “ %e is in scientific\n” , f );

Of course, if you are to do this with integers, you must convert them to floating point first:
int i = 10000;
printf( “ %e a scientific integer.\n” , (float) i );
Here is an example program demonstrating the %e format specifier:

#include <stdio.h>
main()
{
double f = 1.0 / 1000000.0;
int i;
for( i = 0 ; i < 14 ; ++ i )
{
printf( “%f = %e\n” , f , f );
f *= 10.0;
}
return( 0 );
}

Cross Reference:

None.

No comments:

Post a Comment