C program to calculate profit or loss

Write a C program to enter cost price and selling price of a product and check profit or loss. Also calculate total profit or loss using if else. How to calculate profit or loss on any product using if else in C programming. Program to calculate profit and loss of any product in C. Logic to find profit or loss in C program.

Example
Input

Input cost price: 1000
Input selling price: 1500

Output

Profit: 500

Required knowledge

Basic C programming, If else, Basic Mathematics

Logic to find profit or loss

In your primary mathematics classes, you all have learnt about profit and losses. Here goes the basic definitions for profit and loss. If the cost price is greater than selling price then there is a loss otherwise profit. Basic formula to calculate profit and loss
Profit = S.P - C.P (Where S.P is Selling Price and C.P is Cost Price)
Loss = C.P - S.P

After quick recap of profit and losses let us code solution for the program.

Program to calculate profit or loss

/**
 * C program to calculate profit or loss
 */

#include <stdio.h>

int main()
{
    int cp,sp, amt; 
    
    /* Reads cost price and selling price of a product */
    printf("Enter cost price: ");
    scanf("%d", &cp);
    printf("Enter selling price: ");
    scanf("%d", &sp);
    
    if(sp > cp)
    {
        //Profit
        amt = sp - cp;
        printf("Profit = %d", amt);
    }
    else if(cp > sp)
    {
        // Loss
        amt = cp - sp;
        printf("Loss = %d", amt);
    }
    else
    {
        // Neither profit nor loss
        printf("No Profit No Loss.");
    }

    return 0;
}

Output

Enter cost price: 1000
Enter selling price: 1500
Profit = 500

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>