Skip to main content

In our last program we saw How to Calculate Simple Interest in C Programming. In this program, we will make use of if-else block to calculate profit or loss in C. It will also print the profit or loss amount accordingly.This is a problem from Chapter 1: Getting Started of the book, “Let us C” by “Yashwant Kanetkar”.

Question

Write a program to check if a seller has made profit or loss.

Solution to Calculate Profit or Loss in C

Declare four variables for cost price, selling price, profit and loss. Accept cost price & selling price from user using printf() and scanf(). Use if block to check if SP is greater than CP. If selling price is more, then store the difference between selling price and cost price in profit variable and print the profit, else store the difference between cost price and selling price in loss variable & print the loss.

Download the solution:

Download Program

Source Code:

 /*Program to find profit or loss */
 #include<stdio.h>
 #include<conio.h>
 void main()
 {
 int cp, sp, profit, loss;
 clrscr();
 printf("Enter the value of Cost Price & Selling Price: ");
 scanf("%d %d",&cp,&sp);

if(sp>cp)
 {
 profit=sp-cp;
 printf("The seller has made profit of %d",profit);
 }
 else
 {
 loss=cp-sp;
 printf("The seller has suffered loss of %d",loss);
 }
 getch();
 }

 

Output

Enter the value of Cost Price & Selling Price: 350 450
The seller has made profit of 100

Download the solution:

Download Program

Let us know if you are stuck somewhere and need help. You are also free to request some other problem using our comments section or contact form and we will try our best to provide you a solution to your problem. Alternatively, you can also contact us on our social media pages.

Read Also:

Introduction to C Programming

Calculate gross salary in C

Subtracting two numbers in C

Multiplying two numbers in C

Harsh Shah

A highly motivated individual with a hunger to learn and share as much as I can about everything. Masters in Interaction Design with a Bachelors in Computer Science. I have worked with clients across the globe on various design and tech projects. I believe in giving back and that's how ReadMeNow was founded.

Leave a Reply