Who can help me with this program please :*

Pages: 12
Compose a program that will determine all numbers of the form 2abac divisible by n, where n is a given nonzero natural number.Make this program with while and don't use subprograms please. make the program as simple as posibble

2abac means : a number consisting of 5 digits the first digit should be 2 and the second digit equal to the digit 4
Last edited on
Explain what you mean by (2abac).
Give example inputs and outputs.

And above all, show your attempt!
and ask a C++ related question.
My guess is that it means base-10 digits, e.g. 23437 23435 works for n = 109.
So, for a given n, find all (a, b, c) such that 2 * 10^4 + a * 10^3 + b * 10^2 + a * 10^1 + c * 10^0 ≡ 0 (mod n).
But show some effort yourself first.
Last edited on
generate every possible 2abac number then? there are not very many so finding the ones that N can factor would only take a fraction of a second.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
using namespace std;

int main()
{
   string root = "2";
   string digits = "0123456789";
   int n;
   cout << "Enter n: ";   cin >> n;
   for ( char a : digits )
   {
      for ( char b : digits )
      {
          for ( char c : digits )
         {
            string test = root + a + b + a + c;
            if ( !( stoi( test ) % n ) ) cout << test << '\n';
         }
      }
   }
}

Enter n: 109
20601
22127
23435
24743
26160
26269
27577
28885


Oops!
Better:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main()
{
   int n;
   cout << "Enter n: ";   cin >> n;
   for ( int a = 0; a < 10000; a += 1010 )
   {
      for ( int b = 0; b < 1000; b += 100 )
      {
          for ( int c = 0; c < 10; c++ )
         {
            int test = 20000 + a + b + c;
            if ( !( test % n ) ) cout << test << '\n';
         }
      }
   }
}
Look again at the condition i edited
@denispopescuo,
You didn't seriously report me for answering the (flawed?) question that you put up the first time, did you?

What are the limits on your numbers a, b, c? That product will overflow pretty fast. Also, if a, b, c are "given" numbers, and so is n, then it's a bit of a non-problem, don't you think?
Last edited on
sorry, my mistake, 2abac means :
a number consisting of 5 digits the first digit should be 2 and the second digit equal to the digit 4
denispopescuo wrote:
sorry, my mistake, 2abac means :
a number consisting of 5 digits the first digit should be 2 and the second digit equal to the digit 4


That was what most people understood the first time! Then you changed it to something completely different. (EDIT: and now changed it back again.)

Is there a problem with the two solutions already given?
Last edited on
no
your first program looks right but can you make it with while,and without string
Well, the solution here:
http://www.cplusplus.com/forum/general/273884/#msg1181839
doesn't use a string.

You could change any of the for loops to a while loop, but it doesn't seem much of an advantage.

You could check all the multiples of n between 20000 and 29999, finding their digits and writing out those whose second digit matched the fourth. Given the amount of operations, that doesn't seem an advantage.
yes but its obligatory to use while
i need this program with while
can you edit it and make it with while
Some bricks are thicker than others but this one reads like it is at the extremely thick end of the spectrum.
denispopescuo wrote:
yes but its obligatory to use while
i need this program with while
can you edit it and make it with while


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main()
{
   int n;
   cout << "Enter n: ";   cin >> n;
   int test = ( 19999 / n + 1 ) * n;
   while( test < 30000 )
   {
      if ( ( test / 10 - test / 1000 ) % 10 == 0 ) cout << test << '\n';
      test += n;
   }
}


Enter n: 109
20601
22127
23435
24743
26160
26269
27577
28885

Last edited on
thanks man
Pages: 12