Thursday, April 30, 2009

Program with if statement

Exercise 12
A person purchase certain product from a shop, the shop offers a discount of 10%, if the customer purchases the product of more than $25. If the quantities and price per items are input through the keyboard, write a program to calculate the total expenses.


Solution:



/* Program to calculate the total expense */
#include<stdio.h>
#include<conio.h>


void main()
{
int quantity=0;
float rate=0, tot_price;
printf(“Enter the quantity and rate: ”);
scanf(“%d %f”, &quantity, &rate);
tot_price=quantity*rate;
if(tot_price>25)
tot_price=tot_price-(0.1*tot_price);
printf(“\nTherefore your total price = $ %.2f, tot_price);
getch();
}


Explanation: In this program the quantity and the rate is inputted from the user, then multiplying the quantity with the rate, the total price is to be found out. Now comes the comparison part of the program in to the scenario. If the total price of the product comes more than $25, then the discount part takes the active role, 10% discount of the total price of the product is deducted from the total price {also see: if Statement}. And after deducting what the amount left behind is the amount the person has to pay for the purchase of the product.


The output of the program is as follows:


Case 1
Enter the quantity and rate: 6 4
Therefore your total price = $ 24.00


Case 2
Enter the quantity and rate: 6 5
Therefore your total price = $ 27.00


In the first case the total amount is coming $24 which is not more than $25, so there discount is not applicable, but in the second case the total amount is coming $30, which is more than $25, so the discount of 10% is applied on the total price and is deducted from the total price thus a discount of $3 (10% of $30) is deducted from $30 and thus the new total price becomes $27 ($30 - $3).

No comments:

Post a Comment

Please give your valuable comments or queries if you fell its helpful or if you like the contents of the site. Thanks...