w3resource

C Exercises: Test whether a non-negative number is a multiple of 13 or it is one more than a multiple of 13

C-programming basic algorithm: Exercise-19 with Solution

Write a C program that checks if a given non-negative integer is a multiple of 13 or one more than a multiple of 13. For example, if the given integer is 26 or 27, the program will return true, but if it is 25 or 28, the program will return false.

C Code:

#include <stdio.h> // Include standard input/output library
#include <stdlib.h> // Include standard library for additional functions

// Function declaration for 'test' with an integer parameter 'n'
int test(int n);

int main(void){
    // Print the result of calling 'test' with different integer values and format the output
    printf("%d",test(13));
    printf("\n%d",test(14));
    printf("\n%d",test(27));
    printf("\n%d",test(41));
}

// Function definition for 'test'
int test(int n)
{
    // Check if 'n' is divisible by 13 or if the remainder of 'n' divided by 13 is 1
    if (n % 13 == 0 || n % 13 == 1) {
        return 1; // If any of the conditions are met, return 1 (true)
    } else {
        return 0; // If none of the conditions are met, return 0 (false)
    }
}

Sample Output:

1
1
1
0

Explanation:

int test(int n) {
  return n % 13 == 0 || n % 13 == 1;
}

The function takes an integer n as input and returns a boolean value. It checks if n is a multiple of 13 or if n is one more than a multiple of 13. If either condition is true, it returns true; otherwise, it returns false.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.