Paper size calculation program in C

Here’s an example of a paper size calculation program in C that calculates the dimensions of paper sizes based on ISO 216 standard:

#include <stdio.h>

void calculatePaperSize(int size, float width, float height) {
    printf("ISO Paper Size %dA: %.2fmm x %.2fmm\n", size, width, height);

    // Calculate the next smaller size (n-1A)
    if (size > 0) {
        float temp = width;
        width = height;
        height = temp / 2;
        calculatePaperSize(size - 1, width, height);
    }
}

int main() {
    int size;

    // Prompt the user to enter the paper size
    printf("Enter the ISO paper size (A0 - A8): ");
    scanf("%d", &size);

    // Calculate and display the paper size dimensions
    if (size >= 0 && size <= 8) {
        float width = 841;  // A0 width in millimeters
        float height = 1189;  // A0 height in millimeters
        calculatePaperSize(size, width, height);
    } else {
        printf("Invalid ISO paper size. Please try again.\n");
    }

    return 0;
}

To compile and run the program, follow these steps:

  1. Save the code in a file with a .c extension, such as paper_size_calculation.c.
  2. Open a terminal or command prompt and navigate to the directory where the file is saved.
  3. Compile the program using a C compiler (e.g., GCC) by running the following command: gcc -o paper_size_calculation paper_size_calculation.c This will create an executable file named paper_size_calculation.
  4. Run the program by executing the following command: ./paper_size_calculation

The program prompts the user to enter an ISO paper size, ranging from A0 to A8. It then calculates and displays the dimensions (width and height) of the specified paper size, as per the ISO 216 standard. Additionally, it recursively calculates the dimensions of the next smaller paper size (n-1A) until reaching A8.

Feel free to modify and enhance the program as per your specific requirements.

Posted in: C