C Program To Calculate Profit or Loss

In this post, we will learn how to calculate profit or loss using C Programming language.

C Program To Calculate Profit or Loss

If a product is sold at a price more than its cost price then the seller makes a Profit. For example: a plot was purchased at Rs 75,000 and six months later it was sold at 1,00,000 then there is a profit of Rs 25,000.

If a product is sold at a price less than its cost price then the seller makes a loss. For example: a phone was purchased at Rs 20,000 and four months later it was sold at 12,000 then there is a loss of Rs 8000.

We will be using the same logic in our program to calculate profit or loss.

So, without further ado, let’s begin the tutorial.

C Program To Calculate Profit or Loss

// C Program To Calculate Profit or Loss
#include <stdio.h>
int main(){
    float cost_price, sold_price, amount;
    
    // Asking for Input
    printf("Enter the Actual Cost: ");
    scanf("%f", &cost_price);
    printf("Enter the Selling Cost: ");
    scanf("%f", &sold_price);
    
    if (sold_price > cost_price){
        amount = sold_price - cost_price;
        printf("There is a profit of %.2f.", amount);
    }
    else if (cost_price > sold_price){
        amount = cost_price - sold_price;
        printf("There is a loss of %.2f.", amount);
    }
    else{
        printf("There is neither profit nor loss.");
    }
    return 0;
}

Output 1

Enter the Actual Cost: 35
Enter the Selling Cost: 50
There is a profit of 15.00.

Output 2

Enter the Actual Cost: 48
Enter the Selling Cost: 27
There is a loss of 21.00.

How Does This Program Work ?

    float cost_price, sold_price, amount;

In this program, we have declared three float data type variables named cost_price, sold_price and amount.

    // Asking for Input
    printf("Enter the Actual Cost: ");
    scanf("%f", &cost_price);
    printf("Enter the Selling Cost: ");
    scanf("%f", &sold_price);

Then, the user is asked to enter the values of actual price of the product and selling price of the product.

    if (sold_price > cost_price){
        amount = sold_price - cost_price;
        printf("There is a profit of %.2f.", amount);
    }

Then, we check whether SP(Sold Price) > CP (Cost Price). If yes, then there is a profit of SP – CP amount.

    else if (cost_price > sold_price){
        amount = cost_price - sold_price;
        printf("There is a loss of %.2f.", amount);
    }

And if CP > SP, then there is a loss of CP – SP amount.

    else{
        printf("There is neither profit nor loss.");
    }

If neither of the two statements are correct, then it means SP = CP, in this case there is neither profit nor loss.

Conclusion

I hope after going through this post, you understand how to calculate profit or loss using C Programming language.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to help you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *