In this tutorial you will learn about the C program to find Profit or Loss and its application with practical example.
C program to find Profit or Loss
In this tutorial, we will learn to create a C program that will find Profit or Loss in C programming.
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- Conditional Statements in c programming.
What is the Profit or Loss?
The profit means the extra amount received from the buyer for the product, rather than that of its actual price. If the product is sold at the price below the cost price, then it will be taken as a loss in that product.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declare the variables for the program. 2. Taking the input cost and the selling price from the user. 3. Checking both the values using the conditional statement. 4. Passing the values to required functions for calculations. 5. After the calculations, we will print the profit loss for that product. 6. End the program. |
Program to find Profit or Loss.
In this program, we will first take the cost price and the selling price from the user. After taking the input, we will find whether the cost price is equal, greater, or lesser than the selling price. Now we will use the required functions to calculate the profit/loss for the product. At last, we will print the output to the user using the printf(); function.
Below is an example of profit-loss calculation.
1 2 3 4 |
For Example :- cost price = 500 selling price = 1000 profit/loss = selling price - cost price |
With the help of this program, we can find Profit or Loss.
Program Code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
/* C program to find Profit or Loss */ #include <stdio.h> //Calculating the profit and loss //Function will return profit int Profit(int cp, int sp){ return (sp - cp); } // Function will return Loss. int Loss(int cp, int sp){ return (cp - sp); } //Main function. int main() { //Declaring the required variable for the program. int cp, sp; //cp = it will hold input value for the cost price. //sp = it will hold input value for the cost price. //taking input cost price for the program. printf("Enter the cost price of the product "); scanf("%d", &cp); //taking input selling price for the program. printf("Enter the selling price of the product "); scanf("%d", &sp); //Calculating the profit and loss if (sp == cp) //printing No profit nor Loss. printf("No profit nor Loss\n"); else if (sp > cp) //printing the profit. printf("%d Profit\n", Profit(cp, sp)); else //printing the loss. printf("%d Loss\n", Loss(cp, sp)); return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- cp = it will hold the input value of the cost price.
- sp = it will hold the input value of the cost price.
Taking the input cost price and the selling price from the user.
In this section of the code of the program, we will calculate the profit and the loss for the user.
Printing the roots of the given numbers by the user.