Codiqo

Learn C · lesson 12 of 60 · about 8 minutes

Project: Backup Day

Decide when the report runs a full backup: read the day with scanf and turn the rule into %, ||, && and !.

What you'll learn

  • Translate rules into && and ||
  • Use % to test divisibility
  • Read input and report a result

The backup rule

Your report now decides when to run a full backup. The rule:

1. A full backup runs every 7th day of the month, and on the 1st
2. But never when space is low

Every 7th day means the day divides by 7 exactly: day % 7 == 0. In C:

(day % 7 == 0 || day == 1) && !low_space

The rule, on its own

#include <stdio.h>
#include <stdbool.h>

int main(void) {
    int day;
    printf("Day: ");
    scanf("%d", &day);
    bool low_space = false;
    bool full_backup = (day % 7 == 0 || day == 1) && !low_space;
    printf("Full backup on day %d: %d\n", day, full_backup);
    return 0;
}

Output

Day: Full backup on day 14: 1

Test the edges

Rules break at their edges. Day 7 should run a backup and day 8 shouldn't; day 1 is the special case; and low space stops even day 14. Check each one before you trust the rule.

Tip

The brackets matter: without them, && is worked out first, and the 7th, 14th and so on would run a backup even with low space. gcc warns about mixing && and || without brackets.

Now try it yourself

This lesson is free in Codiqo, with 9 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.