Using ifs to decide on a discount : If « Language Basics « C / ANSI-C






Using ifs to decide on a discount


#include <stdio.h>

void main() {

  const double price = 3.50;                /* price*/
  
  int quantity = 0;
  
  printf("Enter the number that you want to buy:");     /* Prompt message */
  
  scanf(" %d", &quantity);                             /* Read the input */

   /* Test for order quantity qualifying for a discount */
   if( quantity>20)                                     /* 5% discount */                
   
     printf("The price for %d is $%.2f\n", quantity, quantity * price * 0.95);
      
   else
                                                    /* No discount */
     printf("The price for %d is $%.2f\n", quantity, quantity * price); 
}


           
       








Related examples in the same category

1.A simple example of the if statementA simple example of the if statement
2.IF statement 3
3.If else
4.More statement in if else
5.If else if and else
6.More if else
7.Block if else statementBlock if else statement
8.Use two if statements
9.Use result as the if condition
10.Use user input as the if condition
11.Use int as if condition
12.If statement for different choice
13.Use calculation in if statement
14.if command
15.Use if else
16.Use statement inside if else
17.Move more statements into if and else
18.If else statement inside a for loop
19.If statement inside for loop