C Online Compiler
Example: Profit and Profit Margin Calculator in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Profit and Profit Margin Calculator #include <stdio.h> int main() { // Step 1: Declare variables for revenue, expenses, profit, and profit margin. float revenue; float expenses; float profit; float profitMargin; // Step 2: Prompt user for total revenue. printf("Enter total revenue: $"); scanf("%f", &revenue); // Step 3: Prompt user for total expenses. printf("Enter total expenses: $"); scanf("%f", &expenses); // Step 4: Calculate profit. profit = revenue - expenses; // Step 5: Calculate profit margin only if revenue is not zero to avoid division by zero. if (revenue != 0) { profitMargin = (profit / revenue) * 100; } else { profitMargin = 0.0; // If no revenue, no margin. } // Step 6: Display the calculated profit and profit margin. printf("\n--- Profitability Report ---\n"); printf("Total Revenue: $%.2f\n", revenue); printf("Total Expenses: $%.2f\n", expenses); printf("----------------------------\n"); printf("Net Profit: $%.2f\n", profit); printf("Profit Margin: %.2f%%\n", profitMargin); // Display as percentage return 0; }
Output
Clear
ADVERTISEMENTS