Multiplying numbers is a pretty common thing in programming. Whether you're adding totals or doing some scientific math, you gotta know how to handle different data types right. In this program, we’ll multiply two floating-point numbers using C. Let’s check it out.
Prerequisites for the C Program to Multiply Two Floating-Point Numbers
To write this C program, you'll need the following:
- Understanding of
floatdata type in C. - Basic arithmetic operations in C.
printfandscanffunctions for input and output.- Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler.
Source Code of C Program to Multiply Two Floating-Point Numbers
Here's the complete code for multiplying two floating-point numbers:
#include <stdio.h>
int main() {
float a, b, product;
// Input two floating-point numbers
printf("Enter the first number: ");
scanf("%f", &a);
printf("Enter the second number: ");
scanf("%f", &b);
// Calculate the product
product = a * b;
// Display the result
printf("Product = %.2f\n", product);
return 0;
}Try this code yourself — free
Run it in the EduSeekho online compiler. No setup needed.
Output of the Multiply Two Floating-Point Numbers Program
When you run the program, you can input two floating-point numbers to see their product.
Enter the first number: 5.5
Enter the second number: 2.1
Product = 11.55
Algorithm to Multiply Two Floating-Point Numbers in C
In this program, you input two numbers, multiply them, and print the result. Here's the step-by-step logic:
- Read two floating-point numbers from the user.
- Multiply the numbers.
- Print the product.
flowchart TD A([Start]) --> B[/Read the first floating-point number a/] B --> C[/Read the second floating-point number b/] C --> D[product = a * b] D --> E[/Print product/] E --> F([End])
How the C Program to Multiply Two Floating-Point Numbers Works
Let's understand how each line of the code works to multiply two floating-point numbers.
#include <stdio.h>: This includes the standard input-output library for functions likeprintfandscanf.float a, b, product;: Declares three floating-point variables to hold the input numbers and their product.printf("Enter the first number: ");: Prompts the user to input the first number.scanf("%f", &a);: Reads the first number into variablea.printf("Enter the second number: ");: Prompts the user to input the second number.scanf("%f", &b);: Reads the second number into variableb.product = a * b;: Multiplies the two numbers and stores the result inproduct.printf("Product = %.2f\n", product);: Prints the product, formatted to two decimal places.return 0;: Indicates that the program executed successfully.
Quick Quiz — test your understanding
Tap an option to check your answerWhat is the data type used for floating-point numbers in this program?
How Fast Is the C Program to Multiply Two Floating-Point Numbers?
Let's delve into the complexity of our program.
Time Complexity: O(1). The operation multiplies two numbers directly with no loops or recursion. It performs a fixed amount of work regardless of the input numbers.
Space Complexity: O(1). Only three variables are used: a, b, and product, which are floating-point numbers.
Common mistake:Forgetting to use the right format specifier %f for floating-point input and output can mess things up or cause errors.
Key takeaway:Multiplying floating-point numbers in C is easy when you use the right data types and format specifiers.
FAQs About C Program to Multiply Two Floating-Point Numbers
Let's answer some common questions about this program.
float or double to work with floating-point numbers.float takes up less memory than double, but double gives you more precision. Go with float if you need smaller precision.double or bump up the precision in the printf format specifier, like %.4f for four decimal places.float type. Think about using double for a larger range.Related Programs
If you found this program helpful, check out these related programs:
- C Program to Compute Quotient and Remainder Easily
- C Program to Generate Multiplication Table with Example
- C Program to Count Number of Digits in an Integer
- C Program to Reverse a Number with Example
- C Program to Check Whether a Number is Palindrome or Not
Conclusion
Multiplying floating-point numbers in C is a breeze, but precision's important. Watch out for issues like overflow and rounding. For an extra challenge, try expanding this program to deal with more numbers or switch to using double for more precision.
Test your skills by tackling more C problems in our C language quiz, and practice coding using our online compiler.