C Program To Calculate Profit or Loss

If cost price and selling price of an item are input through the keyboard, write a C program to determine whether the seller has made profit or incurred loss. Also determine how much profit he made or loss he incurred.

Related Read:
else if statement in C
Relational Operators In C

Note:
Cost Price: is the price paid by the seller for the product or the manufacturing cost of the product.

Selling Price: is the price seller sold the product for.

Formula To Calculate Profit

profit = (selling_price – cost_price);

Formula To Calculate Loss

loss = (cost_price – selling_price);

Expected Output for the Input

User Input:
Enter the cost price of the product
1500
Enter the selling price of the product
2000

Output:
Your profit is 500

Video Tutorial: C Program To Calculate Profit or Loss



YouTube Link: https://www.youtube.com/watch?v=ZjMNJNxKXuE [Watch the Video In Full Screen.]

Source Code: C Program To Calculate Profit or Loss

#include < stdio.h >

int main()
{
    int cp, sp;

    printf("Enter the cost price of the product\n");
    scanf("%d", &cp);

    printf("Enter the selling price of the product\n");
    scanf("%d", &sp);

    if(sp > cp)
    {
        printf("Your profit is %d\n", (sp-cp));
    }
    else if(cp > sp)
    {
        printf("Loss Incurred is %d\n", (cp-sp));
    }
    else
    {
        printf("Neither profit, nor loss\n");
    }

    return 0;
}

Output 1:
Enter the cost price of the product
2000
Enter the selling price of the product
2500
Your profit is 500

Output 2:
Enter the cost price of the product
2000
Enter the selling price of the product
1500
Loss Incurred is 500

Output 3:
Enter the cost price of the product
1500
Enter the selling price of the product
1500
Neither profit, nor loss

Logic To Calculate Profit or Loss

We ask the user to enter cost price as well as selling price. If selling price is more than cost price, then the seller is in profit. If the cost price is more than selling price, then the seller incurred loss.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert