123456789101112131415161718192021222324252627282930313233
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { srand(time(NULL)); int num = rand() % 100; int guess = -1; int trycount = 0; while (guess != num && trycount < 8) { cout << "Enter your guess: "; cin >> guess; if (guess < num) { cout << "Wrong! You entered the smaller number! Try again!\n"; } if (guess > num) { cout << "Wrong! You enter a higher number! Try again!\n"; } trycount++; } if (guess == num) { cout << "You're right! Woo hoo! Hello smart guy! :)\n"; } else { cout << "You fool! The number is " << num << endl; } }
12345678910111213141516171819202122
#include <iostream> #include <cstdlib> #include <ctime> int main() { std::srand( std::time(nullptr) ); const int num = std::rand() % 100 ; int guess ; for( int trycount = 0 ; std::cout << "Enter your guess: " && std::cin >> guess && guess != num && trycount < 8 ; ++trycount ) { if (guess < num) std::cout << "Wrong! You entered the smaller number! Try again!\n"; else std::cout << "Wrong! You enter a higher number! Try again!\n"; } if (guess == num) std::cout << "You're right! Woo hoo! Hello smart guy! :)\n"; else std::cout << "You fool! The number is " << num << '\n' ; }
123456789101112
#include <iostream> int main() { for( int n : { 0, 2, 100, 700, 567, 1789 } ) { std::cout << n << " % 100 == " << n % 100 << '\n' << -n << " % 100 == " << -n % 100 << '\n' << n << " % -100 == " << n % -100 << '\n' << -n << " % -100 == " << -n % -100 << "\n\n" ; } }