Print values of 'a' in equation (a+b) <= n and a+b is divisible by x

Given an equation where we need to find values of 'a' such that a+b ? n and (a+b) is divisible by x. This problem involves finding all valid values of 'a' that satisfy both the sum constraint and divisibility condition.

Syntax

for (divisible = (b / x + 1) * x; divisible <= n; divisible += x) {
    if (divisible - b >= 1) {
        // divisible - b gives us the value of 'a'
    }
}

Algorithm

START
Step 1 ? Declare variables b=10, x=9, n=40 and flag=0, divisible
Step 2 ? Loop For divisible = (b / x + 1) * x and divisible ? n and divisible += x
   IF divisible - b ? 1
      Print divisible - b
      Set flag = 1
   End
END
STOP

Example

Here's a complete program that finds all values of 'a' satisfying the given conditions −

#include <stdio.h>

int main() {
    int b = 10, x = 9, n = 40, flag = 0;
    int divisible;
    
    printf("Values of 'a' where (a+b) <= %d and (a+b) is divisible by %d:<br>", n, x);
    
    for (divisible = (b / x + 1) * x; divisible <= n; divisible += x) {
        if (divisible - b >= 1) {
            printf("%d ", divisible - b);
            flag = 1;
        }
    }
    
    if (flag == 0) {
        printf("No valid values found");
    }
    
    return 0;
}
Values of 'a' where (a+b) <= 40 and (a+b) is divisible by 9:
8 17 26

How It Works

  • We start from the first multiple of x that is greater than b: (b / x + 1) * x
  • For each valid divisible value, we calculate a = divisible - b
  • We ensure a ? 1 to get positive values only
  • The loop continues while divisible ? n to satisfy the constraint a+b ? n

Conclusion

This algorithm efficiently finds all values of 'a' by iterating through multiples of x and checking the constraints. The time complexity is O(n/x) and it handles the divisibility condition systematically.

Updated on: 2026-03-15T11:09:02+05:30

183 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements