Q:

C program to calculate profit or loss

0

C program to calculate profit or loss

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.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C programming Basic Input, Output, if else, Ternary Operator based Programs

Similar questions


need a help?


find thousands of online teachers now
Calculate the distance between two cities from kil... >>
<< printf() examples/variations in C...