How I can go it?

Using the code, I determine the numbers divisible by a given divisor, but you need to output what exactly these numbers are.
How to do it?

int GCD(int a, int b)

{
while (b != 0)
{
int t = b;
b = a % b;
a = t;
}
return a;

}
int main()
{
setlocale(LC_ALL, "ru");
int num, start,c,d;
cout << "Введите размер массива:" << endl;
cin >> num;
cout << "Введите начальное значение:" << endl;
cin >> start;
cin >> d;
int* p_arr = new int[num];
for (int i = start; i < num; i++)
{
p_arr[i] = i;
cout << p_arr[i] << endl;
}
for (int i = start; i < num; i++)
{
c = GCD(p_arr[i],d);

cout << c << endl;

}
delete[] p_arr;
return 0;

}
Tell us some example input values (num, start, d), and what you want the output to be, given that input.

If you want to know if a number is divisible by a given divisor, you don't even need gcd, you just need % (modulo).

e.g.
if (num % 3 == 0) { ... } means "if num is divisible by 3".

Edit your posts and format your code properly.
[code] { your program here } [/code]
Last edited on
num is the finite element of the array, start is the number from which the array begins, d is the number divisor. I need to output those numbers that are divisible by a given divisor
Edit your posts and format your code properly.
[code] { your program here } [/code]
Last edited on
Do you mean:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
	int num {}, start {}, d {};

	std::cout << "How many numbers:";
	std::cin >> num;

	std::cout << "Start number, divisor:";
	std::cin >> start >> d;

	for (int i = start; i <= start + num; ++i)
		if (i % d == 0)
			std::cout << i << " ";

	std::cout << '\n';
}



How many numbers:100
Start number, divisor:100 7
105 112 119 126 133 140 147 154 161 168 175 182 189 196

Thanks for the help. I used your output and framed in my code and everything works as it should.
int GCD(int a, int b)

{
while (b != 0)
{
int t = b;
b = a % b;
a = t;
}
return a;

}
int main()
{
setlocale(LC_ALL, "ru");
int num, start,c,d;
cout << "Введите размер массива:" << endl;
cin >> num;
cout << "Введите начальное значение:" << endl;
cin >> start;
cin >> d;
int* p_arr = new int[num];
for (int i = start; i < num; i++)
{
p_arr[i] = i;
cout << p_arr[i] << endl;
}
for (int i = start; i < num; i++)
{
c = GCD(p_arr[i],d);
if (c%d==0)
cout << i << endl;

}
delete[] p_arr;
return 0;

}
Введите размер массива:
21
Введите начальное значение:
1
5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
All right
5
10
15
20
Topic archived. No new replies allowed.