Millimeters to Centimeters Conversion¶
Concept Explanation¶
What is it?¶
This program converts a length value given in millimeters (mm) to its equivalent in centimeters (cm). Millimeters and centimeters are units of length in the metric system, with 1 centimeter being equal to 10 millimeters.
Why is it important?¶
Unit conversion is a common task in various scientific, engineering, and everyday applications. It demonstrates fundamental arithmetic operations and the ability to handle numerical inputs and outputs accurately. In programming, it reinforces the concepts of variable assignment and basic calculations.
Where is it used?¶
- Engineering and Manufacturing: Converting measurements for design, production, and quality control.
- Science: Data analysis and reporting in fields like biology, chemistry, and physics.
- Design and Graphics: Scaling objects or layouts between different units.
- Everyday Tasks: Adjusting measurements in recipes, DIY projects, or when reading maps.
Real-world example¶
If you measure a small object to be 75 millimeters long and need to report its length in centimeters, you would use this conversion. 75 mm converts to 7.5 cm. This is helpful when different contexts or tools use different units of the metric system.
Algorithm¶
- Start.
- Get the length in millimeters (
millimeters) from the user. - Apply the conversion formula:
centimeters = millimeters / 10. - Display the original length in millimeters and the calculated length in centimeters.
- End.
Conversion Formula:
Centimeters = Millimeters / 10
Edge Cases: - Non-numeric input for length (handled by language-specific error mechanisms or explicit validation). - Zero or negative lengths: A length cannot be zero or negative. The program should ideally handle these inputs by requesting valid dimensions.
Implementations¶
import java.util.Scanner;
public class MmToCm {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter length in millimeters (mm): ");
double millimeters = scanner.nextDouble();
// Convert millimeters to centimeters
double centimeters = millimeters / 10;
System.out.println("Millimeters: " + millimeters + " mm");
System.out.println("Centimeters: " + String.format("%.2f", centimeters) + " cm");
scanner.close();
}
}
# Get length in millimeters from user
millimeters_str = input("Enter length in millimeters (mm): ")
millimeters = float(millimeters_str)
# Convert millimeters to centimeters
centimeters = millimeters / 10
print(f"Millimeters: {millimeters} mm")
print(f"Centimeters: {centimeters:.2f} cm") # Format to 2 decimal places
SET SERVEROUTPUT ON;
DECLARE
mm_val NUMBER := &Enter_Length_in_mm;
cm_val NUMBER;
BEGIN
DBMS_OUTPUT.PUT_LINE('--- Millimeters to Centimeters Conversion ---');
DBMS_OUTPUT.PUT_LINE('Millimeters: ' || mm_val || ' mm');
cm_val := mm_val / 10;
DBMS_OUTPUT.PUT_LINE('Centimeters: ' || ROUND(cm_val, 2) || ' cm');
END;
/
Explanation¶
- Java: Uses
doublefor length values.Scannerreads the millimeter input, and the division is performed directly.String.format("%.2f", ...)formats the output. - Python: Reads input as a string and converts to
float. Applies the division and uses an f-string to display formatted output. - C: Reads
doubleinput usingscanf("%lf", ...). The division is performed, andprintf("%.2lf\n", ...)displays the formatted result. - Oracle: Implemented in PL/SQL. Uses
NUMBERdata type for length. Substitution variable (&Enter_Length_in_mm) for input.ROUND()is used for formatting the centimeter output.
Complexity Analysis¶
- Time Complexity: O(1) - The number of arithmetic operations is constant.
- Space Complexity: O(1) - A fixed number of variables are used.
Flowchart¶
graph TD
A[Start] --> B[Get Millimeters]
B --> C[Calculate Centimeters = Millimeters / 10]
C --> D[Display Millimeters and Centimeters]
D --> E[End]
Sample Dry Run¶
| Step | Millimeters | Centimeters | Description |
|---|---|---|---|
| Input | 75.0 | - | User enters 75.0 |
| Process | 75.0 | 7.5 | Centimeters = 75.0 / 10 = 7.5 |
| Output | 75.0 | 7.5 | Display "75.00 mm is 7.50 cm" |
| End | - | - | Program terminates |
Practice Problems¶
Easy¶
- Modify the program to convert centimeters to meters.
- Convert inches to centimeters.
Medium¶
- Implement input validation to ensure positive numbers are entered for length.
- Create a program that converts between different units of weight (e.g., kilograms to pounds).
Hard¶
- Develop a unit conversion utility that can handle multiple unit types (length, mass, volume, temperature) based on user choice.
"The only source of knowledge is experience." - Albert Einstein