c

exercises

exercises.c🔧
/*
 * ============================================================================
 * OPERATORS IN C - EXERCISES
 * ============================================================================
 * Complete these exercises to master C operators.
 * 
 * Compile: gcc -Wall exercises.c -o exercises
 * Run: ./exercises
 * ============================================================================
 */

#include <stdio.h>

/*
 * ============================================================================
 * EXERCISE 1: Arithmetic Operators
 * ============================================================================
 * Difficulty: Easy
 * 
 * Task: Given two numbers, calculate and print:
 * - Sum, Difference, Product, Quotient, Remainder
 * 
 * Expected Output:
 *   a = 17, b = 5
 *   Sum: 22
 *   Difference: 12
 *   Product: 85
 *   Quotient: 3
 *   Remainder: 2
 */
void exercise_1() {
    printf("\n=== Exercise 1: Arithmetic Operators ===\n");
    
    int a = 17, b = 5;
    
    // TODO: Calculate and print each result
    
    
}

/*
 * ============================================================================
 * EXERCISE 2: Increment/Decrement
 * ============================================================================
 * Difficulty: Medium
 * 
 * Task: Predict the output, then verify by running.
 * 
 * Expected Output:
 *   Initial x = 5
 *   1. y = x++; x = ?, y = ?
 *   2. After reset, y = ++x; x = ?, y = ?
 *   3. z = x-- + ++y; x = ?, y = ?, z = ?
 */
void exercise_2() {
    printf("\n=== Exercise 2: Increment/Decrement ===\n");
    
    int x = 5, y, z;
    
    printf("Initial x = %d\n", x);
    
    // TODO: Step 1 - Post increment
    // y = x++;
    // printf what are x and y?
    
    
    // TODO: Step 2 - Reset x to 5, then pre-increment
    // x = 5;
    // y = ++x;
    // printf what are x and y?
    
    
    // TODO: Step 3 - Complex expression (x=6, y=6 from step 2)
    // z = x-- + ++y;
    // printf what are x, y, and z?
    
    
}

/*
 * ============================================================================
 * EXERCISE 3: Relational Operators
 * ============================================================================
 * Difficulty: Easy
 * 
 * Task: Compare three numbers and determine their relationships.
 * 
 * Expected Output:
 *   a = 10, b = 20, c = 10
 *   a == b: 0
 *   a == c: 1
 *   a != b: 1
 *   a < b: 1
 *   a > b: 0
 *   a <= c: 1
 *   a >= c: 1
 */
void exercise_3() {
    printf("\n=== Exercise 3: Relational Operators ===\n");
    
    int a = 10, b = 20, c = 10;
    
    // TODO: Print all comparisons
    
    
}

/*
 * ============================================================================
 * EXERCISE 4: Logical Operators
 * ============================================================================
 * Difficulty: Medium
 * 
 * Task: Implement these conditions using logical operators:
 * 1. A person can vote if age >= 18
 * 2. A person can drive if age >= 16 AND has_license
 * 3. A person needs parental consent if age < 18 OR not employed
 * 
 * Test with: age = 17, has_license = 1, is_employed = 0
 */
void exercise_4() {
    printf("\n=== Exercise 4: Logical Operators ===\n");
    
    int age = 17;
    int has_license = 1;
    int is_employed = 0;
    
    printf("Age: %d, Has License: %d, Is Employed: %d\n\n", 
           age, has_license, is_employed);
    
    // TODO: Implement and print each condition
    // Can vote?
    
    // Can drive?
    
    // Needs parental consent?
    
    
}

/*
 * ============================================================================
 * EXERCISE 5: Bitwise Operators
 * ============================================================================
 * Difficulty: Medium
 * 
 * Task: Perform bitwise operations on the given numbers.
 * 
 * a = 12 (binary: 1100)
 * b = 10 (binary: 1010)
 * 
 * Expected Output:
 *   a & b = 8  (1000)
 *   a | b = 14 (1110)
 *   a ^ b = 6  (0110)
 *   ~a = -13 (in two's complement)
 *   a << 2 = 48
 *   a >> 2 = 3
 */
void exercise_5() {
    printf("\n=== Exercise 5: Bitwise Operators ===\n");
    
    int a = 12, b = 10;
    
    printf("a = %d (binary: 1100)\n", a);
    printf("b = %d (binary: 1010)\n\n", b);
    
    // TODO: Print results of all bitwise operations
    
    
}

/*
 * ============================================================================
 * EXERCISE 6: Bit Flags
 * ============================================================================
 * Difficulty: Hard
 * 
 * Task: Use bitwise operators to manage permission flags.
 * 
 * Permissions:
 *   READ    = bit 0 (1)
 *   WRITE   = bit 1 (2)
 *   EXECUTE = bit 2 (4)
 * 
 * Steps:
 * 1. Start with no permissions (0)
 * 2. Add READ permission
 * 3. Add WRITE permission
 * 4. Check if EXECUTE permission exists
 * 5. Remove WRITE permission
 * 6. Toggle READ permission
 */
void exercise_6() {
    printf("\n=== Exercise 6: Bit Flags ===\n");
    
    // Define permission bits
    #define READ    (1 << 0)  // 1
    #define WRITE   (1 << 1)  // 2
    #define EXECUTE (1 << 2)  // 4
    
    unsigned int permissions = 0;
    
    // TODO: Implement each step and print the permissions value
    // Step 1: Start with 0
    printf("Initial: %d\n", permissions);
    
    // Step 2: Add READ
    
    // Step 3: Add WRITE
    
    // Step 4: Check EXECUTE
    
    // Step 5: Remove WRITE
    
    // Step 6: Toggle READ
    
    
}

/*
 * ============================================================================
 * EXERCISE 7: Assignment Operators
 * ============================================================================
 * Difficulty: Easy
 * 
 * Task: Use compound assignment operators.
 * Start with x = 100, apply operations in sequence.
 * 
 * Expected:
 *   x += 20  → 120
 *   x -= 30  → 90
 *   x *= 2   → 180
 *   x /= 3   → 60
 *   x %= 7   → 4
 */
void exercise_7() {
    printf("\n=== Exercise 7: Assignment Operators ===\n");
    
    int x = 100;
    printf("Initial x = %d\n\n", x);
    
    // TODO: Apply each operation and print result
    
    
}

/*
 * ============================================================================
 * EXERCISE 8: Ternary Operator
 * ============================================================================
 * Difficulty: Medium
 * 
 * Task: Rewrite these if-else statements using ternary operator.
 * 
 * 1. Find the maximum of two numbers
 * 2. Find the minimum of two numbers
 * 3. Check if a number is positive, negative, or zero
 * 4. Convert a boolean to "Yes" or "No"
 */
void exercise_8() {
    printf("\n=== Exercise 8: Ternary Operator ===\n");
    
    int a = 15, b = 25;
    int num = -5;
    int flag = 1;
    
    // TODO: 1. Find max using ternary
    // int max = ?
    
    
    // TODO: 2. Find min using ternary
    // int min = ?
    
    
    // TODO: 3. Check positive/negative/zero (nested ternary)
    // char *sign = ?
    
    
    // TODO: 4. Convert boolean to string
    // char *answer = ?
    
    
}

/*
 * ============================================================================
 * EXERCISE 9: Operator Precedence
 * ============================================================================
 * Difficulty: Medium
 * 
 * Task: Without running the code, predict the result of each expression.
 * Then verify by running.
 * 
 * Expressions:
 * 1. 2 + 3 * 4 - 1
 * 2. 10 / 2 + 3 * 2
 * 3. 15 % 4 * 3 + 1
 * 4. 5 > 3 && 2 < 4
 * 5. 5 & 3 == 1  (tricky!)
 */
void exercise_9() {
    printf("\n=== Exercise 9: Operator Precedence ===\n");
    
    // TODO: First write your predictions, then verify
    
    printf("Expression 1: 2 + 3 * 4 - 1\n");
    printf("Predicted: ?\n");
    printf("Actual: %d\n\n", 2 + 3 * 4 - 1);
    
    printf("Expression 2: 10 / 2 + 3 * 2\n");
    printf("Predicted: ?\n");
    printf("Actual: %d\n\n", 10 / 2 + 3 * 2);
    
    printf("Expression 3: 15 %% 4 * 3 + 1\n");
    printf("Predicted: ?\n");
    printf("Actual: %d\n\n", 15 % 4 * 3 + 1);
    
    printf("Expression 4: 5 > 3 && 2 < 4\n");
    printf("Predicted: ?\n");
    printf("Actual: %d\n\n", 5 > 3 && 2 < 4);
    
    printf("Expression 5: 5 & 3 == 1 (tricky!)\n");
    printf("Predicted: ?\n");
    printf("Actual: %d\n", 5 & 3 == 1);
    printf("Correct way: (5 & 3) == 1 = %d\n", (5 & 3) == 1);
}

/*
 * ============================================================================
 * EXERCISE 10: Simple Calculator
 * ============================================================================
 * Difficulty: Medium
 * 
 * Task: Create a simple calculator that:
 * - Takes two numbers and an operator
 * - Performs the operation
 * - Handles division by zero
 */
void exercise_10() {
    printf("\n=== Exercise 10: Simple Calculator ===\n");
    
    int num1 = 20;
    int num2 = 5;
    char operator = '*';  // Change to test: +, -, *, /, %
    int result;
    
    printf("%d %c %d = ", num1, operator, num2);
    
    // TODO: Use switch or if-else to calculate based on operator
    // Handle division by zero for / and %
    // Print the result
    
    
}

/*
 * ============================================================================
 * BONUS: Power of 2 Checker
 * ============================================================================
 * Difficulty: Hard
 * 
 * Task: Check if a number is a power of 2 using bitwise operators.
 * 
 * Hint: A power of 2 has only one bit set (e.g., 8 = 1000).
 * n & (n-1) equals 0 only for powers of 2 (and 0).
 */
void bonus_exercise() {
    printf("\n=== BONUS: Power of 2 Checker ===\n");
    
    // TODO: Create a condition to check if n is a power of 2
    // Test with numbers: 1, 2, 4, 6, 8, 16, 15, 32
    
    int test_numbers[] = {1, 2, 4, 6, 8, 16, 15, 32, 0};
    int size = sizeof(test_numbers) / sizeof(test_numbers[0]);
    
    for (int i = 0; i < size; i++) {
        int n = test_numbers[i];
        // int is_power_of_2 = ?  // Your condition here
        // printf("%d is %sa power of 2\n", n, is_power_of_2 ? "" : "NOT ");
    }
}

/*
 * ============================================================================
 * MAIN FUNCTION
 * ============================================================================
 */
int main() {
    printf("╔════════════════════════════════════════════════╗\n");
    printf("║    OPERATORS IN C - EXERCISES                  ║\n");
    printf("║    Complete each exercise to practice          ║\n");
    printf("╚════════════════════════════════════════════════╝\n");
    
    // Uncomment each exercise as you complete it
    
    // exercise_1();
    // exercise_2();
    // exercise_3();
    // exercise_4();
    // exercise_5();
    // exercise_6();
    // exercise_7();
    // exercise_8();
    // exercise_9();
    // exercise_10();
    // bonus_exercise();
    
    printf("\n=== Uncomment exercises in main() to run them ===\n");
    
    return 0;
}

/*
 * ============================================================================
 * ANSWER KEY
 * ============================================================================
 * 
 * Exercise 1:
 *   printf("Sum: %d\n", a + b);
 *   printf("Difference: %d\n", a - b);
 *   printf("Product: %d\n", a * b);
 *   printf("Quotient: %d\n", a / b);
 *   printf("Remainder: %d\n", a % b);
 * 
 * Exercise 2:
 *   y = x++;  // x = 6, y = 5
 *   y = ++x;  // x = 6, y = 6
 *   z = x-- + ++y;  // x = 5, y = 7, z = 13 (6 + 7)
 * 
 * Exercise 4:
 *   int can_vote = (age >= 18);
 *   int can_drive = (age >= 16 && has_license);
 *   int needs_consent = (age < 18 || !is_employed);
 * 
 * Exercise 5:
 *   printf("a & b = %d\n", a & b);   // 8
 *   printf("a | b = %d\n", a | b);   // 14
 *   printf("a ^ b = %d\n", a ^ b);   // 6
 *   printf("~a = %d\n", ~a);         // -13
 *   printf("a << 2 = %d\n", a << 2); // 48
 *   printf("a >> 2 = %d\n", a >> 2); // 3
 * 
 * Exercise 6:
 *   permissions |= READ;           // Add READ
 *   permissions |= WRITE;          // Add WRITE
 *   if (permissions & EXECUTE)     // Check EXECUTE
 *   permissions &= ~WRITE;         // Remove WRITE
 *   permissions ^= READ;           // Toggle READ
 * 
 * Exercise 8:
 *   int max = (a > b) ? a : b;
 *   int min = (a < b) ? a : b;
 *   char *sign = (num > 0) ? "positive" : (num < 0) ? "negative" : "zero";
 *   char *answer = flag ? "Yes" : "No";
 * 
 * Exercise 9:
 *   1. 2 + 3 * 4 - 1 = 2 + 12 - 1 = 13
 *   2. 10 / 2 + 3 * 2 = 5 + 6 = 11
 *   3. 15 % 4 * 3 + 1 = 3 * 3 + 1 = 10
 *   4. 5 > 3 && 2 < 4 = 1 && 1 = 1
 *   5. 5 & 3 == 1 = 5 & (3 == 1) = 5 & 0 = 0 (TRICKY!)
 *      (5 & 3) == 1 = 1 == 1 = 1 (CORRECT)
 * 
 * Exercise 10:
 *   switch (operator) {
 *       case '+': result = num1 + num2; break;
 *       case '-': result = num1 - num2; break;
 *       case '*': result = num1 * num2; break;
 *       case '/': 
 *           if (num2 != 0) result = num1 / num2;
 *           else printf("Error: Division by zero\n");
 *           break;
 *       case '%':
 *           if (num2 != 0) result = num1 % num2;
 *           else printf("Error: Division by zero\n");
 *           break;
 *   }
 * 
 * BONUS:
 *   int is_power_of_2 = (n > 0) && ((n & (n - 1)) == 0);
 * 
 * ============================================================================
 */
Exercises - C Programming Tutorial | DeepML