Skip to content

Solutions

These are all the solutions for our example pseudo code problem, including the complete pseudo code it self.

Pseudo Code

// Initialize variables
SET total_products TO 0
SET total_revenue TO 0.0
SET total_profit TO 0.0
SET most_profitable_factory TO 0
SET highest_factory_revenue TO 0.0

// Ask user for the number of factories with basic validation
PRINT "Enter the number of factories (a positive number): "
READ factories_count

WHILE factories_count IS EMPTY OR factories_count < 1 DO
    PRINT "Invalid input. Please enter a positive number."
    PRINT "Enter the number of factories (a positive number): "
    READ factories_count
ENDWHILE

FOR factory_number FROM 1 TO factories_count DO
    PRINT "Enter the number of products for Factory ", factory_number, " (a positive number): "
    READ products_count

    WHILE products_count IS EMPTY OR products_count < 1 DO
        PRINT "Invalid input. Please enter a positive number."
        PRINT "Enter the number of products for Factory ", factory_number, " (a positive number): "
        READ products_count
    ENDWHILE

    SET factory_revenue TO 0.0
    SET factory_profit TO 0.0

    FOR product_number FROM 1 TO products_count DO
        PRINT "Enter the manufacturing cost for Product ", product_number, " in Factory ", factory_number, " (a non-negative number): "
        READ manufacturing_cost

        WHILE manufacturing_cost IS EMPTY OR manufacturing_cost < 0 DO
            PRINT "Invalid input. Please enter a non-negative number."
            PRINT "Enter the retail price for Product ", product_number, " in Factory ", factory_number, " (a non-negative number): "
            READ manufacturing_cost
        ENDWHILE

        PRINT "Enter the retail price for Product ", product_number, " in Factory ", factory_number, " (a non-negative number): "
        READ retail_price

        WHILE retail_price IS EMPTY OR retail_price < 0 DO
            PRINT "Invalid input. Please enter a non-negative number."
            PRINT "Enter the retail price for Product ", product_number, " in Factory ", factory_number, " (a non-negative number): "
            READ retail_price
        ENDWHILE

        // Update total products and factory revenue
        SET total_products TO total_products + 1
        SET factory_revenue TO factory_revenue + retail_price

        // Calculate the profits on this product
        SET factory_profit TO factory_profit + (retail_price - manufacturing_cost)
    ENDFOR

    // Update total revenue
    SET total_revenue TO total_revenue + factory_revenue
    SET total_profit TO total_profit + factory_profit

    // Check for the most profitable factory
    IF factory_revenue > highest_factory_revenue THEN
        SET highest_factory_revenue TO factory_revenue
        SET most_profitable_factory TO factory_number
    ENDIF
ENDFOR

// Print the report
PRINT "Total Factories: ", factories_count
PRINT "Total Products: ", total_products
PRINT "Total Revenue: $", total_revenue
PRINT "Total Profit: $", total_profit
PRINT "Most Profitable Factory: Factory ", most_profitable_factory

Python

# Initialize variables
total_products = 0
total_revenue = 0.0
total_profit = 0.0
most_profitable_factory = 0
highest_factory_revenue = 0.0

# Ask user for the number of factories with validation
print("Enter the number of factories (a positive number): ")
while True:
    try:
        factories_count = int(input())
        if factories_count > 0:
            break
        else:
            print("Invalid input. Please enter a positive number.")
    except ValueError:
        print("Invalid input. Please enter a valid integer.")
    print("Enter the number of factories (a positive number): ")

for factory_number in range(1, factories_count + 1):
    print(f"Enter the number of products for Factory {factory_number} (a positive number): ")
    while True:
        try:
            products_count = int(input())
            if products_count > 0:
                break
            else:
                print("Invalid input. Please enter a positive number.")
        except ValueError:
            print("Invalid input. Please enter a valid integer.")
        print(f"Enter the number of products for Factory {factory_number} (a positive number): ")

    factory_revenue = 0.0
    factory_profit = 0.0

    for product_number in range(1, products_count + 1):
        print(f"Enter the manufacturing cost for Product {product_number} in Factory {factory_number} (a non-negative number): ")
        while True:
            try:
                manufacturing_cost = float(input())
                if manufacturing_cost >= 0:
                    break
                else:
                    print("Invalid input. Please enter a non-negative number.")
            except ValueError:
                print("Invalid input. Please enter a valid number.")
            print(f"Enter the manufacturing cost for Product {product_number} in Factory {factory_number} (a non-negative number): ")

        print(f"Enter the retail price for Product {product_number} in Factory {factory_number} (a non-negative number): ")
        while True:
            try:
                retail_price = float(input())
                if retail_price >= 0:
                    break
                else:
                    print("Invalid input. Please enter a non-negative number.")
            except ValueError:
                print("Invalid input. Please enter a valid number.")
            print(f"Enter the retail price for Product {product_number} in Factory {factory_number} (a non-negative number): ")

        # Update total products and factory revenue
        total_products += 1
        factory_revenue += retail_price
        # Calculate the profits on this product
        factory_profit += (retail_price - manufacturing_cost)

    # Update total revenue and profit
    total_revenue += factory_revenue
    total_profit += factory_profit

    # Check for the most profitable factory
    if factory_revenue > highest_factory_revenue:
        highest_factory_revenue = factory_revenue
        most_profitable_factory = factory_number

# Print the report
print(f"Total Factories: {factories_count}")
print(f"Total Products: {total_products}")
print(f"Total Revenue: ${total_revenue:.2f}")
print(f"Total Profit: ${total_profit:.2f}")
print(f"Most Profitable Factory: Factory {most_profitable_factory}")

Golang

package main

import (
    "bufio"
    "fmt"
    "os"
    "strconv"
    "strings"
)

func main() {
    scanner := bufio.NewScanner(os.Stdin)

    // Initialize variables
    var totalProducts int
    var totalRevenue, totalProfit, highestFactoryRevenue float64
    var mostProfitableFactory int

    // Ask user for the number of factories with validation
    fmt.Printf("Enter the number of factories (a positive number): ")
    var factoriesCount int
    for {
        scanner.Scan()
        input := strings.TrimSpace(scanner.Text())
        var err error
        factoriesCount, err = strconv.Atoi(input)
        if err == nil && factoriesCount > 0 {
            break
        }
        fmt.Println("Invalid input. Please enter a positive number.")
    }

    for factoryNumber := 1; factoryNumber <= factoriesCount; factoryNumber++ {
        fmt.Printf("Enter the number of products for Factory %d (a positive number): ", factoryNumber)
        var productsCount int
        for {
            scanner.Scan()
            input := strings.TrimSpace(scanner.Text())
            var err error
            productsCount, err = strconv.Atoi(input)
            if err == nil && productsCount > 0 {
                break
            }
            fmt.Println("Invalid input. Please enter a positive number.")
        }

        var factoryRevenue, factoryProfit float64
        for productNumber := 1; productNumber <= productsCount; productNumber++ {
            fmt.Printf("Enter the manufacturing cost for Product %d in Factory %d (a non-negative number): ", productNumber, factoryNumber)
            var manufacturingCost float64
            for {
                scanner.Scan()
                input := strings.TrimSpace(scanner.Text())
                var err error
                manufacturingCost, err = strconv.ParseFloat(input, 64)
                if err == nil && manufacturingCost >= 0 {
                    break
                }
                fmt.Println("Invalid input. Please enter a non-negative number.")
            }

            fmt.Printf("Enter the retail price for Product %d in Factory %d (a non-negative number): ", productNumber, factoryNumber)
            var retailPrice float64
            for {
                scanner.Scan()
                input := strings.TrimSpace(scanner.Text())
                var err error
                retailPrice, err = strconv.ParseFloat(input, 64)
                if err == nil && retailPrice >= 0 {
                    break
                }
                fmt.Println("Invalid input. Please enter a non-negative number.")
            }

            // Update total products and factory revenue
            totalProducts++
            factoryRevenue += retailPrice
            // Calculate the profits on this product
            factoryProfit += (retailPrice - manufacturingCost)
        }

        // Update total revenue and profit
        totalRevenue += factoryRevenue
        totalProfit += factoryProfit

        // Check for the most profitable factory
        if factoryRevenue > highestFactoryRevenue {
            highestFactoryRevenue = factoryRevenue
            mostProfitableFactory = factoryNumber
        }
    }

    // Print the report
    fmt.Printf("Total Factories: %d\n", factoriesCount)
    fmt.Printf("Total Products: %d\n", totalProducts)
    fmt.Printf("Total Revenue: $%.2f\n", totalRevenue)
    fmt.Printf("Total Profit: $%.2f\n", totalProfit)
    fmt.Printf("Most Profitable Factory: Factory %d\n", mostProfitableFactory)
}

C

Warning

This code hasn't been tested for security concerns or even developed with them in mind.

To use this code, you can compile it using gcc:

gcc -o your_program your_program.c

And then you can execute it as such:

./your_program

Or on Windows:

.\your_program

Now for the code itself:

#include <stdio.h>

int main() {
    int factoriesCount, factoryNumber, productsCount, productNumber, totalProducts = 0, mostProfitableFactory = 0;
    double manufacturingCost, retailPrice, factoryRevenue, totalRevenue = 0.0, factoryProfit, totalProfit = 0.0, highestFactoryRevenue = 0.0;

    // Ask user for the number of factories with validation
    printf("Enter the number of factories (a positive number): ");
    while (1) {
        if (scanf("%d", &factoriesCount) == 1 && factoriesCount > 0) {
            break;
        } else {
            printf("Invalid input. Please enter a positive number.\n");
            while (getchar() != '\n'); // Clear input buffer
            printf("Enter the number of factories (a positive number): ");
        }
    }

    for (factoryNumber = 1; factoryNumber <= factoriesCount; factoryNumber++) {
        printf("Enter the number of products for Factory %d (a positive number): ", factoryNumber);
        while (1) {
            if (scanf("%d", &productsCount) == 1 && productsCount > 0) {
                break;
            } else {
                printf("Invalid input. Please enter a positive number.\n");
                while (getchar() != '\n'); // Clear input buffer
                printf("Enter the number of products for Factory %d (a positive number): ", factoryNumber);
            }
        }

        factoryRevenue = 0.0;
        factoryProfit = 0.0;

        for (productNumber = 1; productNumber <= productsCount; productNumber++) {
            printf("Enter the manufacturing cost for Product %d in Factory %d (a non-negative number): ", productNumber, factoryNumber);
            while (1) {
                if (scanf("%lf", &manufacturingCost) == 1 && manufacturingCost >= 0) {
                    break;
                } else {
                    printf("Invalid input. Please enter a non-negative number.\n");
                    while (getchar() != '\n'); // Clear input buffer
                    printf("Enter the manufacturing cost for Product %d in Factory %d (a non-negative number): ", productNumber, factoryNumber);
                }
            }

            printf("Enter the retail price for Product %d in Factory %d (a non-negative number): ", productNumber, factoryNumber);
            while (1) {
                if (scanf("%lf", &retailPrice) == 1 && retailPrice >= 0) {
                    break;
                } else {
                    printf("Invalid input. Please enter a non-negative number.\n");
                    while (getchar() != '\n'); // Clear input buffer
                    printf("Enter the retail price for Product %d in Factory %d (a non-negative number): ", productNumber, factoryNumber);
                }
            }

            totalProducts++;
            factoryRevenue += retailPrice;
            factoryProfit += (retailPrice - manufacturingCost);
        }

        totalRevenue += factoryRevenue;
        totalProfit += factoryProfit;

        if (factoryRevenue > highestFactoryRevenue) {
            highestFactoryRevenue = factoryRevenue;
            mostProfitableFactory = factoryNumber;
        }
    }

    printf("Total Factories: %d\n", factoriesCount);
    printf("Total Products: %d\n", totalProducts);
    printf("Total Revenue: $%.2f\n", totalRevenue);
    printf("Total Profit: $%.2f\n", totalProfit);
    printf("Most Profitable Factory: Factory %d\n", mostProfitableFactory);

    return 0;
}