Float Product
Create a simple C# console application that multiplies two floating-point numbers and displays the result.
Solution
Following snippet of code is a basic calculator program that prompts the user to enter two numbers, multiplies them together, and displays the product formatted to three decimal places.
cs
namespace FloatProduct
{
internal class Program
{
static void Main(string[] args)
{
Console.Write("x = ");
var str = Console.ReadLine();
var x = Convert.ToSingle(str);
Console.Write("y = ");
str = Console.ReadLine();
var y = Convert.ToSingle(str);
var res = x * y;
Console.WriteLine("x * y = {0:f3}", res);
}
}
}Features
- Accepts two floating-point number inputs from the user
- Performs multiplication operation
- Displays the result formatted to 3 decimal places
- Simple and straightforward console interface
Usage
- Run the application
- When prompted, enter the first number (x) and press Enter
- Enter the second number (y) and press Enter
- The program will display the product in the format:
x * y = [result]
Example
x = 5.5
y = 2.3
x * y = 12.650Building and Running
Using Visual Studio
- Open the project in Visual Studio
- Press F5 or click "Start" to build and run
Using .NET CLI
bash
dotnet runCode Structure
The application consists of a single Program class with a Main method that:
- Reads two string inputs from the console
- Converts them to single-precision floating-point numbers using
Convert.ToSingle() - Multiplies the numbers
- Outputs the result with 3 decimal places using string formatting
Source Code
Please refer to the GitHub repository.