How many in the interval [a; b] are the numbers that are divisible by 7 without remainder
Write a program that reads numbers a
, b
(100 <a, b <10000)
from the input data and determines how many in the interval [a; b] are numbers that are divisible by 7 without remainder. Show them on the screen and count them
#include <iostream>
using namespace std;
int main() {
int a, b;
int sum=0;
cin >> a >> b;
for (int i = a; i <= b; ++i) {
if (i % 7 ==0) {
i++;
cout << sum << ' ';
}
}
return 0;
}
3
Author: MSDN.WhiteKnight, 2018-06-19
4 answers
Code:
#include <iostream>
using namespace std;
int main() {
int a, b;
int number = 0;
cin >> a >> b;
for (int i = a; i <= b; i++) {
if (i % 7 == 0) {
number++;
cout << i << ' ';
}
}
cout << "\n";
cout << "number: " << number << "\n";
return 0;
}
Link to the code to play with: https://ideone.com/ctv16G
2
Author: gil9red, 2018-06-19 15:36:12
int deltaA = (a % 7 == 0)? 0 : (7 - (a % 7));
int count = (b - (b % 7) - (a + deltaA)) / 7 + 1;
if (count < 0)
count = 0;
function bySeven(a, b) {
var deltaA = (a % 7 == 0)? 0 : (7 - (a % 7));
var count = (b - (b % 7) - (a + deltaA) ) / 7 + 1;
return Math.max(count, 0);
}
console.log(bySeven(43, 25));
console.log(bySeven(43, 48));
console.log(bySeven(25, 43));
console.log(bySeven(25, 30));
console.log(bySeven(14, 14));
console.log(bySeven(13, 14));
console.log(bySeven(14, 15));
4
Author: Igor, 2018-06-19 15:58:59
Align a
to a border multiple of 7, with rounding up
a = (a + 6) / 7 * 7;
Align b
to a border multiple of 7, with rounding down
b = b / 7 * 7;
Calculate the number of numbers that are multiples of 7 in the resulting interval [a, b]
(assuming that a <= b
)
n = (b - a) / 7 + 1;
1
Author: AnT, 2018-06-22 19:07:12
#include <iostream>
short a, b, c;
int main() {
std::cin >> a >> b;
for(a = a; a <= b; a++) {
if(a % 7 == 0) {
std::cout << a << ' ';
c++;
}
}
std::cout << std::endl << c;
}
1
Author: Cormentor, 2018-06-23 07:23:06