C Program to Multiply Two Floating Point Numbers

In the C programming language, numbers can be categorized into different data types depending on whether they contain decimal values or not. While integers are used to represent whole numbers such as 5, 20, or -3, many real-world calculations require numbers that contain fractional or decimal parts. For example, values like 2.5, 10.75, and 0.333 are not integers but floating-point numbers. To work with these values in C, we use the float data type.

This tutorial explains how to write a simple C program that multiplies two floating-point numbers entered by the user. The program will accept two decimal numbers as input, calculate their product using the multiplication operator, and then display the result on the screen. Even though the logic is simple, this example introduces several important programming concepts that beginners must understand, including variable declaration, floating-point input, arithmetic operations, and formatted output.

By the end of this tutorial, you will understand how floating-point numbers work in C, how to read decimal values using scanf, how arithmetic operations are performed on float variables, and how to display results with the printf function.

Understanding Floating Point Numbers in C

Floating-point numbers are used to represent numbers that contain decimal points or fractions. In C programming, these numbers are stored using special data types designed to handle decimal values. The most commonly used floating-point types are float, double, and long double. Each of these types differs in terms of memory usage and precision.

The float data type is the most basic floating-point type and is commonly used in beginner programs. It typically occupies 4 bytes of memory and can store decimal numbers with about 6 to 7 digits of precision. Because of its simplicity and efficiency, it is often used when high precision is not required.

For example, if you want to store numbers like 2.5, 3.14, or 9.876 in a C program, you would declare a variable using the float keyword. These values can then be used in mathematical calculations such as addition, subtraction, multiplication, and division.

Why Use Float Instead of Int

The int data type is used only for whole numbers. If you attempt to store decimal numbers using int, the fractional part will be lost. For example, if you store the value 2.75 in an integer variable, only the value 2 will be retained.

In many practical situations, decimal values are essential. Consider calculations involving measurements, scientific computations, currency conversions, or engineering formulas. These scenarios require the ability to represent fractional values accurately. This is where floating-point types such as float become necessary.

Using float ensures that decimal numbers are preserved and calculations remain accurate. When multiplying numbers like 2.5 and 4.0, the result should be 10.0, which can only be represented correctly using floating-point variables.

Multiplication Operator in C

In C programming, multiplication is performed using the asterisk (*) operator. This operator is used between two operands to calculate their product. The operands can be integers, floating-point numbers, or even variables that store numeric values.

For example, if you have two variables named a and b, the expression a * b multiplies their values. The result of the multiplication can then be stored in another variable or printed directly.

In our program, we will read two float values from the user, multiply them using the * operator, and store the result in a third float variable.

Reading Floating Point Numbers Using scanf

The scanf function is used in C to read input from the user through the keyboard. When reading floating-point numbers, the %f format specifier must be used. This tells the compiler that the input value should be interpreted as a float.

For example, the statement scanf("%f", &a); reads a floating-point number and stores it in the variable a. The ampersand symbol (&) is required because scanf needs the memory address of the variable where the input will be stored.

If we want to read two floating-point numbers at once, we can use two %f format specifiers separated by a space. The user can then enter both values separated by a space or newline.

Printing Floating Point Numbers Using printf

The printf function is used to display output on the screen. To print floating-point numbers, the %f format specifier is used. By default, %f prints the value with six digits after the decimal point.

For example, if the result of multiplication is 10, printf("%f", result); will display 10.000000. This behavior can be modified if fewer decimal places are required.

If you want to limit the output to two decimal places, you can use %.2f instead of %f. This formatting technique is useful when displaying user-friendly output in real-world applications.

Algorithm to Multiply Two Floating Point Numbers

  1. Start the program.
  2. Declare three float variables to store the input numbers and their product.
  3. Prompt the user to enter two floating-point numbers.
  4. Use scanf with %f format specifiers to read the values.
  5. Multiply the two numbers using the * operator.
  6. Store the result in a third variable.
  7. Display the result using printf.
  8. End the program.

C Program

C
#include <stdio.h>

int main() {
    float a, b, c;

    printf("Enter 2 numbers: ");

    // Read two floating point numbers
    scanf("%f %f", &a, &b);

    // Multiply the numbers
    c = a * b;

    // Print the result
    printf("Product of the two numbers: %f", c);

    return 0;
}

Sample Output

Enter 2 numbers: 2.5 4.0
Product of the two numbers: 10.000000

Step-by-Step Explanation of the Program

The program begins by including the stdio.h header file. This header file contains the declarations for input and output functions such as printf and scanf. Without including this file, the program would not be able to use these functions.

Next, the main function is defined. In C, every program starts execution from the main function. This function acts as the entry point for the program.

Inside the main function, three variables named a, b, and c are declared using the float data type. The variables a and b will store the numbers entered by the user, while c will store the result of the multiplication.

The printf function is then used to display a message asking the user to enter two numbers. This message helps the user understand what input the program expects.

The scanf function reads the two floating-point numbers from the keyboard. The format string "%f %f" tells scanf to read two float values. The addresses of variables a and b are passed using the & operator so that the entered values can be stored correctly.

After the input is received, the program multiplies the two values using the expression c = a * b;. The multiplication operator calculates the product and stores the result in the variable c.

Finally, the printf function displays the product of the two numbers using the %f format specifier.

Example Calculations

  • If the user enters 2.5 and 4.0, the result will be 10.000000.
  • If the user enters 1.5 and 3.2, the result will be 4.800000.
  • If the user enters 10.5 and 2.0, the result will be 21.000000.

Improving Output Formatting

Sometimes displaying six decimal places may not be necessary. If you want a cleaner output, you can limit the number of decimal digits displayed.

C
printf("Product: %.2f", c);

The above statement prints the result with only two digits after the decimal point.

Common Mistakes Beginners Make

  • Forgetting to include the stdio.h header file.
  • Using %d instead of %f when reading float values.
  • Forgetting the & symbol before variable names in scanf.
  • Using int instead of float when decimal numbers are required.
  • Not initializing variables properly in larger programs.

Practice Exercises

  • Modify the program to multiply three floating-point numbers.
  • Write a program that multiplies numbers and displays the result with two decimal places.
  • Create a program that multiplies numbers entered separately instead of on the same line.
  • Try using double instead of float and compare the results.

Conclusion

Multiplying two floating-point numbers in C is a simple yet important beginner exercise. It demonstrates how decimal numbers are stored, how user input is handled, and how arithmetic operations are performed in the language. Understanding these basic concepts will help you build more advanced programs involving mathematical calculations, scientific computations, and real-world applications.

By practicing programs like this, beginners can develop a strong foundation in C programming. Once you are comfortable with float variables, input/output functions, and arithmetic operators, you will be ready to explore more complex topics such as conditional statements, loops, and functions.

Note: Note: To limit the number of decimal places in the output, you can use format specifiers like %.2f, %.3f, or %.4f depending on how precise you want the result to appear.