Q5. Prompts the user to enter the quantity of items and the price per item.Calculates the total expense.Applies a 10% discount if the quantity exceeds 1000.Displays the total expense.

				
					#include<stdio.h>
#include<conio.h>
void main()
{
int quant;
float amt,price,total,disc;
clrscr();
printf("How many quantity\n");
scanf("%d",&quant);
printf("Enter the price per item\n");
scanf("%f",&price);
disc=0;
if(quant>1000)
{
disc=10*(quant*price)/100;
total=quant*price-disc;
}
else
total=quant*price;
printf("Total expense is:%0.2f",total);
getch();
}

				
			
Here's the refined code with some improvements and a detailed explanation:
				
					#include <stdio.h> // Standard input/output library
#include <conio.h> // Console input/output library (for clrscr() and getch())

void main() {
    int quant; // Variable to store quantity
    float price, total, disc; // Variables to store price per item, total cost, and discount
    clrscr(); // Clear the screen

    // Prompt user for quantity and price per item
    printf("Enter the quantity: ");
    scanf("%d", &quant);

    printf("Enter the price per item: ");
    scanf("%f", &price);

    // Initialize discount to 0
    disc = 0;

    // Calculate discount if quantity exceeds 1000
    if (quant > 1000) {
        disc = 10 * (quant * price) / 100;
    }

    // Calculate total cost after applying discount if any
    total = quant * price - disc;

    // Display the total expense
    printf("Total expense is: %.2f", total);

    getch(); // Wait for a key press before closing the console
}

				
			

Improvements and Corrections

  1. Code Readability and Structure:

    • Added comments to explain the purpose of each section of the code.
    • Improved variable names to be more descriptive.
  2. Calculation Logic:

    • The discount calculation was correct, but it was moved out of the if statement to avoid redundancy in calculating total.
  3. clrscr and getch:

    • The clrscr function clears the console screen, making the program start with a clean screen. This is typically used in Turbo C/C++ but may not be supported in modern compilers.
    • The getch function is used to wait for a key press before closing the console window, which is useful in a command-line environment to see the output before the window closes.

Note:

  • The conio.h library is specific to some compilers like Turbo C/C++ and may not be available or necessary in modern environments. For portability and modern standards, you might consider removing clrscr() and getch().
  • If using a modern IDE or compiler, you can remove #include <conio.h>, clrscr(), and getch(). Instead, use system("clear") or system("cls") for clearing the screen and getchar() to wait for a key press.

Modified Code for Modern Compilers

				
					#include <stdio.h>

int main() {
    int quant;
    float price, total, disc;

    // Prompt user for quantity and price per item
    printf("Enter the quantity: ");
    scanf("%d", &quant);

    printf("Enter the price per item: ");
    scanf("%f", &price);

    // Initialize discount to 0
    disc = 0;

    // Calculate discount if quantity exceeds 1000
    if (quant > 1000) {
        disc = 10 * (quant * price) / 100;
    }

    // Calculate total cost after applying discount if any
    total = quant * price - disc;

    // Display the total expense
    printf("Total expense is: %.2f\n", total);

    return 0;
}

				
			

In this version, conio.h is removed, and getch() is replaced by a simple return statement, making the code more portable and suitable for modern development environments.

Scroll to Top