C Programming (5th Edition) - Chapter 4 Exercises

4.1 What are arithmetic operations? What are relational operations? What are logical operations?

  • Arithmetic operations: Operations such as addition, subtraction, mutliplication, division, and modulus on numbers.
  • Relational operations: Comparisons between two operands to determine relationships like equality, inequality, greater than, etc.
  • Logical operations: Operations on Boolean values (true/false) to produce a new Boolean value.

4.2 How does C represent 'true' and 'false'? How does the system determine if a value is true or false?

  • C uses 1 to represent true and 0 to represent false.
  • The system treats any non-zero value as true and zero as false.

4.3 Evaluate the following logical expressions given a=3, b=4, c=5.

(1) a+b > c && b == c
(2) a || b+c && b - c
(3) !(a > b) && !c || 1
(4) !(x = a) && (y = b) && 0
(5) !(a + b) + c - 1 && b + c / 2

Answers:

  1. 0
  2. 1
  3. 1
  4. 0
  5. 1

4.4 Write a program to input three integers a, b, c and output the maximum.

#include <stdio.h>

int main() {
    int a, b, c, max;
    printf("Enter three integers: ");
    scanf("%d %d %d", &a, &b, &c);

    max = (a > b) ? a : b;
    max = (max > c) ? max : c;

    printf("Maximum: %d\n", max);
    return 0;
}

4.5 Input a positive number less then 1000, output its integer square root. If the input is not valid, prompt for re-entry.

#include <stdio.h>
#include <math.h>

int main() {
    double num;
    printf("Enter a positive number less than 1000: ");
    scanf("%lf", &num);

    while (num <= 0 || num >= 1000) {
        printf("Invalid input. Please enter again: ");
        scanf("%lf", &num);
    }

    int root = (int)sqrt(num);
    printf("Integer square root: %d\n", root);
    return 0;
}

4.6 Given the piecewise function:

  • y = x (x < 1)
  • y = 2x - 1 (1 <= x < 10)
  • y = 3x - 11 (x >= 10)

Write a program to input x and output y.

#include <stdio.h>

int main() {
    double x, y;
    printf("Enter x: ");
    scanf("%lf", &x);

    if (x < 1) {
        y = x;
    } else if (x < 10) {
        y = 2 * x - 1;
    } else {
        y = 3 * x - 11;
    }

    printf("y = %.2f\n", y);
    return 0;
}

4.7 (Omitted)

4.8 Convert a percentage grade to a letter grade: A (>=90), B (80-89), C (70-79), D (60-69), E (<60).

#include <stdio.h>

int main() {
    float score;
    printf("Enter score: ");
    scanf("%f", &score);

    if (score >= 0 && score <= 100) {
        if (score >= 90) {
            printf("A\n");
        } else if (score >= 80) {
            printf("B\n");
        } else if (score >= 70) {
            printf("C\n");
        } else if (score >= 60) {
            printf("D\n");
        } else {
            printf("E\n");
        }
    } else {
        printf("Invalid score.\n");
    }
    return 0;
}

4.9 For a positive integer with at most 5 digits, determine:

  • Its number of digits
  • Print each digit sequentially
  • Print the digits in reverse order
#include <stdio.h>

int main() {
    int num, digits = 0, temp, reversed = 0;
    printf("Enter a positive integer (<= 5 digits): ");
    scanf("%d", &num);

    temp = num;
    while (temp > 0) {
        digits++;
        temp /= 10;
    }
    printf("Number of digits: %d\n", digits);

    printf("Digits in order: ");
    int divisor = 1;
    for (int i = 1; i < digits; i++) {
        divisor *= 10;
    }
    temp = num;
    while (divisor > 0) {
        printf("%d ", temp / divisor);
        temp %= divisor;
        divisor /= 10;
    }
    printf("\n");

    printf("Reversed: ");
    temp = num;
    while (temp > 0) {
        reversed = reversed * 10 + temp % 10;
        temp /= 10;
    }
    printf("%d\n", reversed);

    return 0;
}

4.10 Calculate bonus based on profit commission tiers:

  • Profit <= 100,000: 10%
  • 100,000 < profit <= 200,000: 10% on first 100,000, 7.5% on excess
  • 200,000 < profit <= 400,000: same for first 200,000, 5% on excess
  • 400,000 < profit <= 600,000: same for first 400,000, 3% on excess
  • 600,000 < profit <= 1,000,000: same for first 600,000, 1.5% on excess
  • Profit > 1,000,000: same for first 1,000,000, 1% on excess
#include <stdio.h>

int main() {
    double profit, bonus = 0;
    printf("Enter profit: ");
    scanf("%lf", &profit);

    if (profit <= 100000) {
        bonus = profit * 0.1;
    } else if (profit <= 200000) {
        bonus = 100000 * 0.1 + (profit - 100000) * 0.075;
    } else if (profit <= 400000) {
        bonus = 100000 * 0.1 + 100000 * 0.075 + (profit - 200000) * 0.05;
    } else if (profit <= 600000) {
        bonus = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + (profit - 400000) * 0.03;
    } else if (profit <= 1000000) {
        bonus = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.03 + (profit - 600000) * 0.015;
    } else {
        bonus = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.03 + 400000 * 0.015 + (profit - 1000000) * 0.01;
    }

    printf("Total bonus: %.2f\n", bonus);
    return 0;
}

4.11 Input four integers and output them in ascending order.

#include <stdio.h>

int main() {
    int arr[4];
    printf("Enter four integers: ");
    for (int i = 0; i < 4; i++) {
        scanf("%d", &arr[i]);
    }

    // Bubble sort
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3 - i; j++) {
            if (arr[j] > arr[j + 1]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }

    printf("Sorted: ");
    for (int i = 0; i < 4; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    return 0;
}

4.12 Four cylindrical towers with centers at (2,2), (-2,2), (-2,-2), (2,-2) and radius 1, height 10m. Input coordinates (x, y), output the height of the building at that point (0 if outside all towers).

#include <stdio.h>
#include <math.h>

int main() {
    double x, y;
    printf("Enter coordinates (x y): ");
    scanf("%lf %lf", &x, &y);

    // Define tower centers
    double centers[4][2] = {{2,2}, {-2,2}, {-2,-2}, {2,-2}};
    double radius = 1.0;
    int inside = 0;

    for (int i = 0; i < 4; i++) {
        double dx = x - centers[i][0];
        double dy = y - centers[i][1];
        if (sqrt(dx*dx + dy*dy) <= radius) {
            inside = 1;
            break;
        }
    }

    if (inside) {
        printf("Height: 10\n");
    } else {
        printf("Height: 0\n");
    }

    return 0;
}

Tags: c programming exercises arithmetic relational logical

Posted on Tue, 12 May 2026 20:49:01 +0000 by kanchan