how to print infinite multiples of 2

Hi Team,

I wrote a code to print multiples of 2 under 100. How can I implement printing infinite multiples other than less than 100? Please help with few suggestions.

#include<iostream>
using namespace std;
int main()
{
int i=0;
while(i<=100)
{
if(i%2==0)
cout<<i<<" ";
i++;
}
system("pause");
return 0;
}
You're printing even numbers, not multiples of 2. Also use code blocks for code snippets.

There is no such thing as doing something infinitely long

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <limits>

using namespace std;

int main()
{
  typedef unsigned int count_t;

  const count_t MAX_COUNT = std::numeric_limits<count_t>::max();
  count_t count = 0;

  while(true && count < MAX_COUNT)
  {
    if (count % 2 == 0)
      cout << count << " ";

    count++;
  }

  system("pause");

  return 0;
}


Computers are not infinite, there's always a limit.
Last edited on
I am actually using Visual Studio 2010. I am trying to write a code for entering multiples of a number into array. Before using array I want to write code for printing infinite multiples of a number. Let's say: below code prints multiples of 7 less than 100. Is there a way to print infinite multiples of number 7?
#include <iostream>
#include <limits>

using namespace std;

int main()
{
int i=0;
while(i<=100)
{
if(i%7==0)
cout<<i<<" ";
i++;
}

system ("pause");
return 0;
}
You're printing even numbers, not multiples of 2.
Umm...even numbers are multiples of 2.
I think you were thinking of powers of 2.

The easy way is to replace while(i<=100) with while (true), but that will overflow once i gets past std::numeric_limits<int>::max().

By the way, you can simplify your original program to just
1
2
3
4
5
int main()
{
    for (int i = 0; i <= 100; i += 2)
        std::cout << i << " ";
}
(Delete the i <= 100 (but not the semicolon after it) if you want to keep going past 100.)
Hey, this looks fun! Can I give my solution? Every number generated is guaranteed to be a multiple of two.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <algorithm>
#include <chrono>
#include <iostream>
#include <limits>
#include <random>

#ifdef _WIN32

  #include <windows.h>
  
  bool IsKeyPressed( unsigned timeout_ms = 0 )
    {
    return WaitForSingleObject(
      GetStdHandle( STD_INPUT_HANDLE ),
      timeout_ms
      ) == WAIT_OBJECT_0;
    }
    
#else // POSIX (presumably)

  #include <poll.h>  // (sorry I forgot this!)

  bool IsKeyPressed( unsigned timeout_ms = 0 )
    {
    struct pollfd pls[ 1 ];
    pls[ 0 ].fd     = STDIN_FILENO;
    pls[ 0 ].events = POLLIN | POLLPRI;
    return poll( pls, 1, timeout_ms ) > 0;
    }

#endif

int main()
  {
  using namespace std;
  
  mt19937 rng( chrono::high_resolution_clock::now().time_since_epoch().count() );
  
  cout << "Generates an infinitely large multiple of two.\n"
          "Press ENTER to start.\n"
	  "Press ENTER again when you think you have got enough digits.\n";
  cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
  
  while (!IsKeyPressed( 10 ))
    {
    char digit = rng() % 10 + '0';
    cout << digit << flush;
    }
  char digit = "02"[ rng() % 2 ];
  cout << digit << endl;

  cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
  }

:O)
Last edited on
Now make it prime.
@Duoas

Wow, seems like you had a lot of fun writing that up! :)

Did you miss an #include or two for the !_WIN32 case?
I get errors for the pollfd stuff.
Whoops! Yes, yes I did.

Hope that fixes it.

@helios
Hmm, that could be an interesting problem...
Last edited on
Topic archived. No new replies allowed.