If ... else statement - C Statement

C examples for Statement:if

Introduction

The syntax of the if-else statement is as follows:

if(expression)
  Statement1;
else
  Statement2;

Next_statement;

Demo Code

#include <stdio.h>

int main(void){
  
  const double unit_price = 3.50;                     // Unit price in dollars
  int quantity = 0;
  
  printf("Enter the number that you want to buy:");   // Prompt message
  scanf(" %d", &quantity);                            // Read the input
  
  double total = 0.0;                                 // Total price
  
  if(quantity > 10)                                   // 5% discount
    total = quantity*unit_price*0.95;/* w  w w .  j  a v  a  2 s  .  c o m*/
  else                                                // No discount
    total = quantity*unit_price;
  
  printf("The price for %d is $%.2f\n", quantity, total);
  return 0;
}

Result


Related Tutorials