This blog is under construction

Monday 8 July 2013

C program to calculate profit and loss

Write a C program to calculate profit or loss percentage.


  #include <stdio.h>
  int main() {
        float sp, cp, profit, loss, gpercent, lpercent;

        /* get the cost price and selling price from user */
        printf("Enter the cost price:");
        scanf("%f", &cp);
        printf("Enter the selling price:");
        scanf("%f", &sp);

        /* if sp == cp, no profit */
        if (sp - cp == 0) {
                printf("Profit & gain percent is 0!!\n");
        } else if ((sp - cp) > 0) {
                /* sp > cp - profit and profit percentage calculation */
                profit = sp - cp;
                gpercent = (profit * 100) / cp;
                printf("Profit = %.2f Rupees\n", profit);
                printf("Gain percentage = %.2f percent\n", gpercent);
        } else {
                /* cp > sp - loss and loss percentage calculation */
                loss = cp - sp;
                lpercent = (loss * 100) / cp;
                printf("Loss = %.2f Rupees\n", loss);
                printf("Loss Percentage = %.2f percent\n", lpercent);
        }
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the cost price:100
  Enter the selling price:128
  Profit = 28.00 Rupees
  Gain percentage = 28.00 percent






See Also:

No comments:

Post a Comment