How to Print Numbers in Scientific Notation in C

Jinku Hu Feb 12, 2024
  1. Introduction to Scientific Notation
  2. Precision and Representation in Scientific Notation
  3. Using %E Format Specifier to Print Numbers in Scientific Notation
  4. Use %e Format Specifier to Print Numbers in Scientific Notation
  5. Use sprintf to Format a String to Print Numbers in Scientific Notation
  6. Create Custom Function to Print Numbers in Scientific Notation
  7. FAQs
  8. Conclusion
How to Print Numbers in Scientific Notation in C

Scientific notation is a powerful and concise way to represent both large and small numerical values using a standardized format with a single digit before the decimal point multiplied by a power of 10.

This article will demonstrate multiple methods for writing and printing numbers in scientific notation in C programming language.

Introduction to Scientific Notation

Scientific notation is a concise and versatile representation employed in programming languages like C to express numbers across varying magnitudes efficiently. It consists of a decimal value, the mantissa, multiplied by 10 raised to a specific exponent, and this notation is especially beneficial for precise representation, accommodating both large and small numbers seamlessly.

In C programming, the double data type facilitates working with floating-point numbers, allowing for extended precision. Through the printf function, scientific notation can be formatted and displayed with optional signs for exponents.

Sample code snippets exemplify the straightforward application of scientific notation in C, emphasizing the significance of precision in mathematical calculations. Key terms such as decimal exponent, floating-point numbers, and significant digits are highlighted, showcasing their importance in numerical representation.

Precision and Representation in Scientific Notation

Scientific notation in C is prized for its precision, adeptly handling numbers of varying magnitudes. Let’s explore how it achieves accuracy and preserves precision in programming.

Decimal Digits and Significant Figures

Scientific notation strategically positions the decimal point after the first digit, preserving significant figures and ensuring precision in representation.

Floating-Point Numbers and Precision

The double data type in C, utilized for floating-point numbers, offers extended precision, crucial for handling real numbers with varying magnitudes.

Optional Signs and Representation

Flexibility in handling positive and negative numbers is provided through optional signs for both decimal value and exponent, ensuring a concise and standardized format.

Measuring Precision in Programming

The printf function in C allows developers to control the precision of scientific notation, catering to the specific needs of mathematical calculations.

Handling Small Numbers and Magnitudes

Scientific notation excels in representing small numbers, maintaining clarity and accuracy in calculations involving very large or very small values.

Let’s now discuss the methods on how to print numbers in Scientific Notation in C.

Using %E Format Specifier to Print Numbers in Scientific Notation

In C, the %E format specifier is employed to print numbers expressed in scientific notation. This specifier is designed for floating-point data types and represents the exponent using the letter 'E' followed by the corresponding sign.

If the exponent is zero, it is displayed as '00'. The following sample code illustrates the use of %E.

Code Example:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
  float n1 = 15000000;
  float n2 = -15000000;
  double n3 = 25.2132;
  double n4 = 0.00001302;

  printf("n1 (%%f) - %f: \n", n1);
  printf("n1 (%%E) - %E: \n", n1);

  printf("n2 (%%f) - %f: \n", n2);
  printf("n2 (%%E) - %E: \n", n2);

  printf("n3 (%%f) - %f: \n", n3);
  printf("n3 (%%E) - %E: \n", n3);

  printf("n4 (%%f) - %f: \n", n4);
  printf("n4 (%%E) - %E: \n", n4);

  exit(EXIT_SUCCESS);
}

In the code, we initialize four variables (n1, n2, n3, and n4) with different numeric values, encompassing both large and small numbers. We then use the printf function to display each variable’s value in two formats: first, using the %f specifier for standard floating-point notation, and second, using the %E specifier for scientific notation.

Output:

scientific notation in c - output 1

In the output, we observe that n1 with a value of 15,000,000 is displayed as 1.500000E+07 when using the %E specifier.

Similarly, n2 with a negative value of -15,000,000 is represented as -1.500000E+07. The %f specifier showcases the standard floating-point notation with the original precision.

Moving on to n3 with a value of 25.2132, the %E specifier displays it as 2.521320E+01 in scientific notation. Lastly, n4 with the smallest value of 0.00001302 is presented as 1.302000E-05 using the %E specifier.

Use %e Format Specifier to Print Numbers in Scientific Notation

Alternatively, the %e format specifier has the exact same features as the previous example, except that the letter displayed is lowercase, in the form of [-]d.ddde±dd. Both %e and %E format specifiers can include a decimal point and an integer on the right side of it to indicate how many places to display.

If the user does not provide the precision integer, by default, it’s automatically set to 6 places. As shown in the following code sample, negative floating-point numbers just get the sign symbol as the usual decimal notation.

Code Example:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
  float n1 = 15000000;
  float n2 = -15000000;
  double n3 = 25.2132;
  double n4 = 0.00001302;

  printf("n1 (%%f) - %f: \n", n1);
  printf("n1 (%%.2e) - %.2e: \n", n1);

  printf("n2 (%%f) - %f: \n", n2);
  printf("n2 (%%.1e) - %.1e: \n", n2);

  printf("n3 (%%f) - %f: \n", n3);
  printf("n3 (%%.2e) - %.2e: \n", n3);

  printf("n4 (%%f) - %f: \n", n4);
  printf("n4 (%%.1e) - %.1e: \n", n4);

  exit(EXIT_SUCCESS);
}

In this code, we initialize four variables (n1, n2, n3, and n4) with diverse numeric values, encompassing both large and small numbers. We then utilize the printf function to display each variable’s value in two formats: firstly, using the %f specifier for standard floating-point notation, and secondly, using the %e specifier for scientific notation.

Additionally, we specify precision for the scientific notation using the %.2e or %.1e format to control the of decimal places displayed.

Output:

scientific notation in c - output 2

In the output, we observe that n1 with a value of 15,000,000 is displayed as 1.50e+07 when using the %e specifier with a precision of two decimal places. Similarly, n2 with a negative value of -15,000,000 is represented as -1.5e+07 with one decimal place precision, and the %f specifier showcases the original precision in standard floating-point notation.

Moving on to n3 with a value of 25.2132, the %e specifier with two decimal places displays it as 2.52e+01 in scientific notation. Finally, n4 with the smallest value of 0.00001302 is presented as 1.3e-05 using the %e specifier with one decimal place precision.

Use sprintf to Format a String to Print Numbers in Scientific Notation

An alternative method involves using the sprintf function to format a string containing the number in scientific notation, which can then be printed. The following code example illustrates this approach:

Code Example 1:

#include <stdio.h>

int main() {
  double number = 123456.789;
  char scientificNotation[50];

  sprintf(scientificNotation, "%e", number);
  printf("Scientific Notation: %s\n", scientificNotation);

  return 0;
}

In this code, we initialize a double-precision variable named number with the value 123456.789. To represent this number in scientific notation, we use the sprintf function to format a string (scientificNotation) using the %e specifier, which is designed for scientific notation output.

The formatted string is then printed using the printf function.

Output:

scientific notation in c - output 3

In the output, we see that the value of the number in scientific notation is represented as 1.234568e+05, where 'e+05' signifies that the decimal point has been shifted five places to the right.

Let’s have another example.

Code Example 2:

#include <stdio.h>

int main() {
  double number1 = 987654.321;
  double number2 = 0.000012345;

  char scientificNotation1[50];
  char scientificNotation2[50];

  sprintf(scientificNotation1, "%e", number1);
  sprintf(scientificNotation2, "%.3e", number2);

  printf("Number 1 in Scientific Notation: %s\n", scientificNotation1);
  printf("Number 2 in Scientific Notation: %s\n", scientificNotation2);

  return 0;
}

In this code, we initialize two double-precision variables, number1 and number2, with the values 987654.321 and 0.000012345, respectively. We then create two character arrays, scientificNotation1 and scientificNotation2, to store the formatted strings representing these two numbers in scientific notation.

Using the sprintf function, we format number1 with the %e specifier and number2 with the %.3e specifier, indicating that we want to display number2 with three decimal places in the exponent part. The formatted strings are stored in the respective character arrays.

Finally, we use printf to output the scientific notation representations of number1 and number2.

Output:

scientific notation in c - output 4

This output demonstrates how the combination of sprintf and printf functions allows us to easily convert and display a numeric value written in scientific notation in a string format.

Create Custom Function to Print Numbers in Scientific Notation

For a more customized approach, a function can be created to convert a number to scientific notation. The following example demonstrates this custom function:

Code Example 1:

#include <stdio.h>

void printScientificNotation(double number) {
  int exponent = 0;
  while (number >= 10.0) {
    number /= 10.0;
    exponent++;
  }

  printf("Scientific Notation: %.2fe%d\n", number, exponent);
}

int main() {
  double number = 123456.789;
  printScientificNotation(number);

  return 0;
}

In this code, we define a custom function named printScientificNotation that takes a double-precision variable number as an argument. Inside the function, we initialize an integer variable exponent to zero.

Using a while loop, we iteratively divide the number of digits by 10 until it becomes less than 10.0, incrementing the exponent with each iteration. This process determines the exponent required for the scientific notation representation.

After calculating the exponent, we use the printf function to output the result in scientific notation format. The format specifier %.2fe%d is used, indicating that the number is displayed with two decimal places and followed by 'e' and the calculated exponent.

In the main function, a double-precision variable number is initialized with the value 123456.789, and the printScientificNotation function is called with this variable as an argument.

Output:

scientific notation in c - output 5

The output of this code is "Scientific Notation: 1.23e5:", showcasing the result of the custom function’s conversion of the original number into scientific notation.

Let’s have another example.

Code Example 2:

#include <stdio.h>

void printScientificNotation(double number) {
  int exponent = 0;
  while (number >= 10.0 || number <= -10.0) {
    number /= 10.0;
    exponent++;
  }

  printf("Scientific Notation: %.4fe%d\n", number, exponent);
}

int main() {
  double number3 = -8765.4321;
  double number4 = 9876543210.0;

  printScientificNotation(number3);
  printScientificNotation(number4);

  return 0;
}

In this code, we define a custom function named printScientificNotation that takes a double-precision variable number as an argument. Inside the function, we initialize an integer variable exponent to zero.

We then enter a while loop that continues until the absolute value of the number is less than 10.0, effectively determining the required for the scientific notation representation. During each iteration, we divide the number by 10 and increment the exponent.

The printf function is then used to output the result in scientific notation format. The format specifier %.4fe%d is used, indicating that the number is displayed with four decimal places and followed by 'e' and the calculated exponent.

In the main function, two double-precision variables, number3 and number4, are initialized with the values -8765.4321 and 9876543210.0, respectively. The printScientificNotation function is called for each of these numbers.

Output:

scientific notation in c - output 6

This output demonstrates the result of the custom function’s conversion of the original numbers into scientific notation. The function effectively calculates the exponent and presents the numbers in a concise and standardized scientific format.

FAQs

Q1: Can I Adjust the Precision of the Scientific Notation Output When Using %e or %E in C?

A1 - Yes, both %E and %e format specifiers in C allow you to control the precision of the scientific notation output. You can specify the precision by adding a dot followed by an integer value in the format specifier.

For example, %E and %e with %.3E or %.3e will display the number in scientific notation with three decimal places. If precision is not explicitly specified, it defaults to six decimal places.

Q2: Can I Print Integers in Scientific Notation in C?

A2 - No, the %E and %e format specifiers in C are designed specifically for floating-point, not integers. Attempting to use these specifiers with integer variables will result in incorrect output.

To represent integers in a similar format, consider converting them to floating-point numbers using explicit casting before applying the scientific notation data format specifier.

Q3: How Do I Handle Very Large or Very Small Numbers When Printing in Scientific Notation?

A3 - C’s %E and %e format specifiers automatically adjust the exponent to represent very large or very small numbers in scientific notation. The precision and formatting of the output can be controlled by specifying the precision in the format specifier.

Additionally, using a custom function allows for more fine-grained control over the handling and calculations of extremely large or small numbers, offering flexibility in the conversion process.

Conclusion

In conclusion, this article discussed the ways to show numbers in a scientific way in C. Scientific notation is like a shorthand for big or small numbers, making them easier to handle in programming.

We explored using %E and %e to format numbers with exponents and also saw how to do this with the sprintf function, turning numbers into strings. Additionally, we created a custom function to convert numbers to scientific notation in a more personalized way.

Whether you’re dealing with tiny or huge numbers, understanding and applying scientific notation in C gives you the tools to handle them accurately. This guide provides you with various methods, letting you choose the one that fits your programming needs best.

Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook