C
c
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
#define _CRT_SECURE_NO_WARNINGS //to avoid warning messages when using scanf
#include <stdio.h> //declaring the libraries to be used
#include <stdlib.h>
void guessing(int guessDigit, int randomNumber) // this function will generate a random 4 - digit number and put it in an array
{
/*******************************************************************
* this code will convert the random number into an array of digits *
********************************************************************/
int randArr[4];
int newRand;
for (int i = 3; i >= 0; i--) //this loop will take each digit from the random number and put it into the suitable array position
{
switch (i) //switch case to put each digit into the suitable array position
{
case 3: randArr[3] = randomNumber % 1000; //the number in the units place will be stored in the 3rd index
newRand = randomNumber / 10; //the random number will be divided by 10 so the previous digit becomes the last one
break;
case 2: randArr[2] = newRand % 100; //the number in the tens place will be stored in the 2nd index
newRand = randomNumber / 10;
break;
case 1: randArr[1] = newRand % 10; //the number in the hundreds place will be stored in the 1st index
newRand = randomNumber / 10;
break;
case 0: randArr[0] = newRand; //the number in the thousands place will be stored in the 0th index. division by 10 is not needed anymore
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run