Nov 25, 2020 at 12:27am UTC
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;
}
Nov 25, 2020 at 1:23am UTC
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 Nov 25, 2020 at 1:25am UTC
Nov 25, 2020 at 1:46am UTC
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
Nov 25, 2020 at 1:57am UTC
Edit your posts and format your code properly.
[ code] { your program here }
[ /code]
Last edited on Nov 25, 2020 at 1:57am UTC
Nov 25, 2020 at 12:27pm UTC
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