How to find the numbers between A and B that can be divided by C number?

423 Views Asked by At

I want to find the numbers between A and B that can be diveded by C using flowgorithm or a math formula.

As of now I can only output the numbers between the two given numbers.

I want to get the results and be able to average them and sum them.

Edit: I am trying to learn maths for obvious reasons and trying to understand the points of it, where to use what and formulas. Even though I tried, I couldn't find an information about how to solve this problem of mine on the internet. So my point is, I don't anyone to write the code for me, I just want to learn how to write it and what is logic behind it as I litteraly have no other sources at the moment. Thanks, if you take your time and help me.

1

There are 1 best solutions below

2
Rory Daulton On

It is hard to write a formula for this problem that will work in all computer languages, since the languages differ in how they treat integer division. Some languages have the modulo operator and some do not. But here is a hint to get started.

You definitely want the first number that is greater than or equal to A that is divisible by C. I'll assume that A, B, and C are positive integers--if that is not the case, things get more complicated and depend more on the computer language. Then the formula is

firstdivisible = ceil(A / C) * C

where ceil is the "ceiling" function--rounding up to an integer. Most languages have this function. Note that you will need to test if this number is greater than B.

You also want the last number in your range that is divisible by C, which is

lastdivisible = floor(B / C) * C

where floor rounds down to an integer. Some languages use int rather than floor. Again, you will need to test if this number is less than A. Some languages have a modulo operator %--if so, you could also use

lastdivisible = B - (B % C)

Given those limits and the spacing C you should be able to create an array with all your desired numbers.