ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

C program to calculate profit or loss

Calculating profit and loss in C: Here, we are going to implement a C program that will calculate the profit and loss based on cost and selling price.
Submitted by Manju Tomar, on July 30, 2019

Here, we are going to calculate how much we gain the profit or loss on a product through cost price and selling price. When we purchase a Car in an "xxx" amount, it is called its cost price and then if we sell the Car in "yyy" amount, it is called its selling price, if "xxx" is greater than "yyy" then it will be a loss and if "yyy" is greater than "xxx", then it will be a profit.

Example: I purchased a house in Rs. 3000000 after that I sold that house after 2 years in Rs. 4000000. The sold price is higher than its purchased price, thus it will be a profit of 1000000.

Calculate profit or loss in C language

#include <stdio.h>

int main()
{
    /*declare the variables*/
    int cp;
    int sp;
    int amount;

    /*enter the cost price*/
    printf("Enter the cost price: ");
    scanf("%d", &cp);

    /*enter the selling price*/
    printf("Enter the selling price: ");
    scanf("%d", &sp);

    /*if condition*/
    if (sp > cp) {
        /*formula to find profit*/
        amount = sp - cp;
        printf("profit = %d", amount);
    }
    /*else if condition*/
    else if (cp > sp) {
        /*formula to find loss*/
        amount = cp - sp;
        printf("loss = %d", amount);
    }
    /*else condition*/
    else {
        printf("No profit no loss\n");
    }
    return 0;
}

Output

First run:
Enter the cost price: 1000 
Enter the selling price: 1500
profit = 500

Second run:
Enter the cost price: 2000 
Enter the selling price: 1500
loss = 500

Third run:
Enter the cost price: 1000 
Enter the selling price: 1000
No profit no loss 

C Basic Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.