w3resource

C Exercises: Calculate profit and loss


18. Profit and Loss Calculation

Write a C program to calculate profit and loss on a transaction.

Visual Presentation:

Calculate profit and loss


For more Practice: Solve these Related Problems:

  • Write a C program to calculate profit or loss percentage from cost and selling prices and display a detailed report.
  • Write a C program to compute profit or loss and output whether the transaction breaks even.
  • Write a C program to calculate profit/loss and determine the break-even point for a series of transactions.
  • Write a C program to accept cost and selling prices, compute profit/loss, and apply a discount factor if applicable.

Sample Solution:

C Code:

#include <stdio.h>  // Include the standard input/output header file.

void main()  
{  
    int cprice, sprice, plamt;  // Declare variables to store cost price, selling price, and profit/loss amount.

    printf("Input Cost Price: ");  // Prompt user for input of cost price.
    scanf("%d", &cprice);  // Read and store the cost price.
    printf("Input Selling Price: ");  // Prompt user for input of selling price.
    scanf("%d", &sprice);  // Read and store the selling price.

    if(sprice > cprice)  // Check if selling price is greater than cost price.
    {  
        plamt = sprice - cprice;  // Calculate profit amount.
        printf("\nYou can book your profit amount : %d\n", plamt);  // Print profit message.
    }  
    else if(cprice > sprice)  // Check if cost price is greater than selling price.
    {  
        plamt = cprice - sprice;  // Calculate loss amount.
        printf("\nYou incurred a loss of amount : %d\n", plamt);  // Print loss message.
    }  
    else  // If neither profit nor loss.
    {  
        printf("\nYou are in a no profit, no loss condition.\n");  // Print message for no profit, no loss.
    }  
}  
 

Output:

Input Cost Price: 500                                                                                         
Input Selling Price: 700                                                                                      
                                                                                                      
You can booked your profit amount : 200 

Flowchart:

Flowchart: Calculate profit and loss


Go to:


PREV : Vowel or Consonant Check.
NEXT : Electricity Bill Calculation.

C Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.