Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

closed i want to find discount but it is not working what i want to do

+1 vote
asked Sep 26, 2021 by RahulPitta (130 points)
closed Oct 5, 2021 by Admin
#include <stdio.h>

void main()
{
      int originalprice ,discount, finalamount;
     
      printf("enter originalprice:");
      
      scanf("%d",&originalprice);

      printf("enter discount:");
      
      scanf("%d",&discount);

     finalamount = originalprice*discount;
     
     printf("%d",finalamount);
  }
closed with the note: answered

2 Answers

0 votes
answered Sep 26, 2021 by TN CODER (290 points)
edited Sep 26, 2021 by TN CODER

Ok that means if you enter "100" in "enter originalprice:" and if you enter "50" in "enter discount:" means it need to show final value like 50.

My  code is here .

(If you need any changes you can edit it)

#include <stdio.h>
#include <conio.h>

void main()
{
   float original_price;
   float discount_amount;
   float discounted_amount;
   float discounted_price;
    printf("\t\t\aWELCOEM TO DISCOUNT CALCULATER BY. TN CODER");
    printf("\n\n\aEnter the original price of the item: ");
    scanf("%f", &original_price);
    printf("\n\aEnter the discount amount of the item: ");
    scanf("%f", &discount_amount);
    discounted_amount = (discount_amount*original_price)/100;
    discounted_price = (original_price-discounted_amount);
    printf("\n\n\aDiscounted amount : %f \n\n",  discounted_amount);
    printf("\aFinal price of the item : %f \n\n",  discounted_price);
}

Don't use integer use float then only it looks good becasue all amount not will be at rouded price like  this "23" , "300" some item price are like this also "23.56" "300.45" . so use float. 

0 votes
answered Sep 26, 2021 by Peter Minarik (101,420 points)
edited Sep 27, 2021 by Peter Minarik

Your problem is with the mathematics.

If you want to calculate the final price (the discount deduced from the original price), then

finalamount = originalprice*discount;

is not the right thing to do.

The correct mathematics would be

float finalamount = originalprice * (1.0f - discount);

, where discount >= 0.0 and discount <= 1.0 (that is a floating-point number between 0.0 and 1.0; if you want percentages, you should divide by 100).

And as TN CODER suggested, use floats so you can calculate fractions, not just the whole (integral) parts.

commented Oct 2, 2021 by TN CODER (290 points)
Yes bro, but from him no replies. .. . . . . .
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...