-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodsoft_1.cpp
More file actions
47 lines (39 loc) · 1.14 KB
/
Copy pathcodsoft_1.cpp
File metadata and controls
47 lines (39 loc) · 1.14 KB
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
// C++ PROGRAM - CODSOFT
// TASK 1 - NUMBER GUESSING GAME
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main (void)
{
int randNum;
// Seeding random number generator
srand(time(NULL));
// To generate a random number starts from 0 to 20
randNum = (rand() % 21);
int guess = 0;
cout << "\nNUMBER GUESSING GAME STARTS NOW!" << endl;
cout << "Dear User,Total Chance=10, ALL THE BEST!" << "\nGuess any number starts from 0 to 20" << endl;
// Loop for 10 chances
int i = 1, result;
while (i <= 10) {
cout << "\nGuess " << i << ": ";
cin >> guess;
// Check for correctness
if (guess == randNum) {
cout << "Congrats, You are correct!";
result = 1;
break;
}
else if (guess < randNum)
cout << "Too Low!";
else
cout << "Too High!";
i++;
}
// Result of the game
if (result == 1)
cout << "\nThe number is " << randNum << ".";
else
cout << "\nOops! You lost.\nPlease Try Again!";
}