Skip to content

← Back to Overview

Centimeters to Millimeters Conversion

Concept Explanation

What is it?

This program converts a length value given in centimeters (cm) to its equivalent in millimeters (mm). Millimeters and centimeters are units of length in the metric system, with 1 centimeter being equal to 10 millimeters. This conversion is the reverse of millimeters to centimeters.

Why is it important?

Unit conversion is a fundamental skill in various fields, ensuring consistency and comparability of measurements. In programming, it provides a straightforward example of applying a conversion factor, handling numerical inputs, and presenting results.

Where is it used?

  • Engineering and Design: Converting between common metric units for precision work or larger scale plans.
  • Crafts and Hobbies: Adjusting patterns or instructions that might be in different metric subunits.
  • Science Education: Illustrating basic metric conversions.

Real-world example

If you are designing a small component and its dimensions are given in centimeters (e.g., a screw with a length of 2.5 cm), but you need to work with a tool that requires measurements in millimeters, you would convert it. 2.5 cm converts to 25 mm.


Algorithm

  1. Start.
  2. Get the length in centimeters (centimeters) from the user.
  3. Apply the conversion formula: millimeters = centimeters * 10.
  4. Display the original length in centimeters and the calculated length in millimeters.
  5. End.

Conversion Formula: Millimeters = Centimeters × 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 CmToMm {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter length in centimeters (cm): ");
        double centimeters = scanner.nextDouble();

        // Convert centimeters to millimeters
        double millimeters = centimeters * 10;

        System.out.println("Centimeters: " + centimeters + " cm");
        System.out.println("Millimeters: " + String.format("%.2f", millimeters) + " mm");

        scanner.close();
    }
}
# Get length in centimeters from user
centimeters_str = input("Enter length in centimeters (cm): ")
centimeters = float(centimeters_str)

# Convert centimeters to millimeters
millimeters = centimeters * 10

print(f"Centimeters: {centimeters} cm")
print(f"Millimeters: {millimeters:.2f} mm") # Format to 2 decimal places
#include <stdio.h>

int main() {
    double centimeters, millimeters;

    printf("Enter length in centimeters (cm): ");
    scanf("%lf", &centimeters);

    // Convert centimeters to millimeters
    millimeters = centimeters * 10;

    printf("%.2lf cm is equal to %.2lf mm\n", centimeters, millimeters);

    return 0;
}
SET SERVEROUTPUT ON;
DECLARE
  cm_val  NUMBER := &Enter_Length_in_cm;
  mm_val  NUMBER;
BEGIN
  DBMS_OUTPUT.PUT_LINE('--- Centimeters to Millimeters Conversion ---');
  DBMS_OUTPUT.PUT_LINE('Centimeters: ' || cm_val || ' cm');

  mm_val := cm_val * 10;

  DBMS_OUTPUT.PUT_LINE('Millimeters: ' || ROUND(mm_val, 2) || ' mm');
END;
/

Explanation

  • Java: Uses double for length values. Scanner reads the centimeter input, and the multiplication is performed directly. String.format("%.2f", ...) formats the output.
  • Python: Reads input as a string and converts to float. Applies the multiplication and uses an f-string to display formatted output.
  • C: Reads double input using scanf("%lf", ...) for centimeters. The multiplication is performed, and printf("%.2lf\n", ...) displays the formatted result.
  • Oracle: Implemented in PL/SQL. Uses NUMBER data type for length. Substitution variable (&Enter_Length_in_cm) for input. ROUND() is used for formatting the millimeter 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 Centimeters]
    B --> C[Calculate Millimeters = Centimeters * 10]
    C --> D[Display Centimeters and Millimeters]
    D --> E[End]

Sample Dry Run

Step Centimeters Millimeters Description
Input 10.0 - User enters 10.0
Process 10.0 100.0 Millimeters = 10.0 * 10 = 100.0
Output 10.0 100.0 Display "10.00 cm is 100.00 mm"
End - - Program terminates

Practice Problems

Easy

  • Modify the program to convert meters to kilometers.
  • Convert feet to inches.

Medium

  • Implement input validation to ensure positive numbers are entered for length.
  • Create a program that converts between different units of volume (e.g., liters to milliliters).

Hard

  • Develop a unit conversion utility that can handle multiple unit types (length, mass, volume, temperature) based on user choice, with a focus on bidirectional conversion.

"The road to success is always under construction." - Lily Tomlin