
Pasha B.
asked 01/18/21Decimal to Binary in c
Between 0 and 255 a number will be decided randomly by computer. Then asks to user 3 times a random digit of binary value of the that number. If user enters wrong number for a digit program will select another random number and ask random times random digit.
Example
Computer selected number as 163
What is the digit 2 (question 1/3) :
User enter 1 Correct
1 Expert Answer

Patrick B. answered 01/18/21
Math and computer tutor/teacher
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define YES (1)
#define NO (0)
Decimal2Binary(int x, char *bitStr)
{
memset(bitStr,'0',8);
bitStr[8]=0; //null terminates
bitStr[0]=x/128+'0';
x%=128;
bitStr[1]=x/64+'0';
x%=64;
bitStr[2]=x/32+'0';
x%=32;
bitStr[3]=x/16+'0';
x%=16;
bitStr[4]=x/8+'0';
x%=8;
bitStr[5]=x/4+'0';
x%=4;
bitStr[6]=x/2+'0';
x%=2;
bitStr[7]=x+'0';
}
int DoItAgainQuestion()
{
int response=-1;
while ((response<0) || (response>1))
{
printf("Another problem ??? 1=YES 0=NO :>");
scanf("%d",&response);
}
return(response);
}
Go()
{
char bitStr[9];
char inbuff[9];
srand(NULL);
int response=YES;
int iLoop=0;
while (response==YES)
{
int randomIntNum = rand()%255;
Decimal2Binary(randomIntNum,bitStr);
char response_flag=1;
for (iLoop=0; iLoop<3; iLoop++)
{
char chBit[2];
printf("the random number is %d = ",randomIntNum);
printf(" binary >%s< \n",bitStr); //<---- COMMENT THIS OUT !!!
int randIndex = rand()%8+1;
printf(" What is digit # %d ??? :>",randIndex);
scanf("%s\n",&chBit);
if (bitStr[randIndex-1]==chBit[0])
{
printf("Yes that is correct...\n");
}
else
{
response_flag=0;
printf(" sorry that is incorrect - try again ! \n\n");
break;
}
}//for loop
if (response_flag==1)
{
response = DoItAgainQuestion();
}
} //while
}
main()
{
Go();
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
PASHA In the RESOURCES section, I uploaded the source code for the program you posted that reverses the strings.01/20/21