w3resource

C Exercises: Calculate profit and loss

C Conditional Statement: Exercise-18 with Solution

Write a C program to calculate profit and loss on a transaction.

Visual Presentation:

Calculate profit and loss

Sample Solution:

C Code:

#include <stdio.h>  // Include the standard input/output header file.

void main()  
{  
    int cprice, sprice, plamt;  // Declare variables to store cost price, selling price, and profit/loss amount.

    printf("Input Cost Price: ");  // Prompt user for input of cost price.
    scanf("%d", &cprice);  // Read and store the cost price.
    printf("Input Selling Price: ");  // Prompt user for input of selling price.
    scanf("%d", &sprice);  // Read and store the selling price.

    if(sprice > cprice)  // Check if selling price is greater than cost price.
    {  
        plamt = sprice - cprice;  // Calculate profit amount.
        printf("\nYou can book your profit amount : %d\n", plamt);  // Print profit message.
    }  
    else if(cprice > sprice)  // Check if cost price is greater than selling price.
    {  
        plamt = cprice - sprice;  // Calculate loss amount.
        printf("\nYou incurred a loss of amount : %d\n", plamt);  // Print loss message.
    }  
    else  // If neither profit nor loss.
    {  
        printf("\nYou are in a no profit, no loss condition.\n");  // Print message for no profit, no loss.
    }  
}  
 

Sample Output:

Input Cost Price: 500                                                                                         
Input Selling Price: 700                                                                                      
                                                                                                      
You can booked your profit amount : 200 

Flowchart:

Flowchart: Calculate profit and loss

C Programming Code Editor:

Previous: Write a C program to check whether an alphabet is a vowel or consonant.
Next: Write a program in C to calculate and print the Electricity bill of a given customer.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.