C (programming language)

If-Else Statements in C Exercises and Solutions

Conquering Choices: Mastering If-Else Statements in C with Exercises and Solutions

C’s if-else statements are your gatekeepers to conditional logic, guiding your program down different paths based on specific conditions. This guide unravels the intricacies of if-else with a selection of exercises and solutions, empowering you to navigate the decision-making world of C programming.

1. The Basics of Branching Out

Exercise 1: Age Verification

Write a program that asks for a user’s age and prints a message based on their age group (child, teenager, adult).

Solution:

C

#include <stdio.h>

int main() {
  int age;

  printf("Enter your age: ");
  scanf("%d", &age);

  if (age < 13) {
    printf("You are a child!\n");
  } else if (age < 20) {
    printf("You are a teenager.\n");
  } else {
    printf("You are an adult!\n");
  }

  return 0;
}

Explanation:

  • We use scanf to read the user’s age into the age variable.
  • An if statement checks if the age is less than 13, printing “child” if true.
  • An else if statement checks for ages between 13 and 19, printing “teenager” if true.
  • Finally, an else block handles all remaining ages, printing “adult”.

2. Nesting for Granular Control

Exercise 2: Grading System

Write a program that takes a score as input and assigns a letter grade based on predefined ranges.

Solution:

C

#include <stdio.h>

int main() {
  float score;

  printf("Enter your score: ");
  scanf("%f", &score);

  if (score >= 90) {
    printf("Grade: A\n");
  } else if (score >= 80) {
    printf("Grade: B\n");
  } else if (score >= 70) {
    printf("Grade: C\n");
  } else if (score >= 60) {
    printf("Grade: D\n");
  } else {
    printf("Grade: F\n");
  }

  return 0;
}

Explanation:

  • This program uses scanf to read the score as a float.
  • A series of nested if statements check the score against different cut-off points, assigning letter grades based on the range it falls within.
  • Each if block prints the corresponding grade if the condition is true.

3. Advanced Techniques for Complex Choices

Exercise 3: Discount Calculator

Write a program that applies different discounts based on the total purchase amount and customer type (regular, premium).

Solution:

C

#include <stdio.h>

int main() {
  float amount;
  char type;

  printf("Enter total purchase amount: ");
  scanf("%f", &amount);

  printf("Enter customer type (R/P): ");
  scanf("%c", &type);

  if (type == 'R') {
    if (amount >= 500) {
      printf("Discount: 10%%\n");
    } else if (amount >= 250) {
      printf("Discount: 5%%\n");
    } else {
      printf("No discount available.\n");
    }
  } else if (type == 'P') {
    if (amount >= 1000) {
      printf("Discount: 15%%\n");
    } else if (amount >= 500) {
      printf("Discount: 10%%\n");
    } else {
      printf("Discount: 5%%\n");
    }
  } else {
    printf("Invalid customer type.\n");
  }

  return 0;
}

Explanation:

  • This program reads the purchase amount and customer type from the user.
  • An if statement checks the customer type.
  • Inside each customer type block, nested if statements check the purchase amount against different levels and print the corresponding discount percentage.
  • An else block in each customer type section handles invalid amounts.
  • An outer else block handles invalid customer types.

Remember, if-else statements are flexible and can be combined in various ways to handle sophisticated decision-making logic. This is just a glimpse into their power. Explore further exercises like:

  • Logical operators: Combine conditions using && (and), || (or), and ! (not) for more complex branching.
  • Switch-case statements: Utilize switch-case for situations with multiple, distinct choices, offering an alternative to nested if-else statements in certain cases.
  • Error handling: Use if-else to validate user input, detect errors, and handle them gracefully.

By mastering if-else statements and practicing with diverse exercises, you’ll not only conquer conditional logic but also unlock the full potential of your C programs.

CodeForHunger

Learn coding the easy way. Find programming guides, examples and solutions with explanations.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button