May 5, 2014 at 11:50pm UTC
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;
}
May 5, 2014 at 11:57pm UTC
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 May 6, 2014 at 12:00am UTC
May 6, 2014 at 12:07am UTC
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;
}
May 6, 2014 at 1:27am UTC
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 May 6, 2014 at 1:36am UTC
May 6, 2014 at 1:34am UTC
@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.
May 6, 2014 at 1:36am UTC
Whoops! Yes, yes I did.
Hope that fixes it.
@helios
Hmm, that could be an interesting problem...
Last edited on May 6, 2014 at 1:37am UTC