Guess a number program with c [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I need to write a program that plays the game "guess the number" when the computer selects the number from 1 to 100, and the user has 10 attempts, each round the user receives output if the number he guessed is too small or too big, or if its the lucky number your help please
thats what i did so far...
#include <stdio.h>
#include <stdlib.h>
void main() {
int i ,guess=0 , lucky=rand()%100;
for (i = 0; i <= 10; i++) {
printf("please enter your guess\n");
scanf("%d",&guess);
}
}

This should work for you!
#include <stdio.h>
#include <stdlib.h>
int main() {
srand(time(NULL));
int count , guess = 0, lucky = rand() % 101;
for (count = 0; count < 10; count++) {
printf("please enter your guess\n>");
scanf("%d", &guess);
if(guess == lucky) {
printf("WIN");
break;
}
if(guess > lucky)
printf("too big\n\n");
else
printf("too small\n\n");
}
if(count == 10)
printf("FAIL");
return 0;
}

Related

The correct ways to use rand() in selection structure [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Can anyone identify why the line : if(code == rand() && attempt != 3) not executed even I'm entered a correct code that generated by the rand().
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
int main(void)
{
int code, attempt = 0;
srand(time(0));
for(int i = 0; i<1; i++)
printf("Your code is %d ", rand());
do{
printf("\nEnter code: ");
scanf("%d", &code);
attempt++;
}while ( code != rand() && attempt != 3);
if(code == rand() && attempt != 3)
{
printf("\n Correct code");
printf("\n");
}
else
{
printf("Incorrect code\n");
}
return 0;
}
The rand function is a random number generator. It returns a different number each time you call it.
Call rand once at the start of your program and save the result in a variable. Then check the user's guess against that.

I am making a prime number checker: when I try to run it, it stopped working [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
#include <stdio.h>
int main() {
int x, i, counter = 0;
printf("Input number!\t");
scanf("%d", &x);
for (i = 0; i <= x; i++) {
if (x % i == 0) {
counter++;
}
}
if (counter <= 2) {
printf("%d is a prime number.", x);
} else {
printf("%d is not a prime number.", x);
}
return 0;
}
It seems the loop part is the problem but I don't know why. I'm very new to programming so please bear with it if its a silly mistake.
Try this Code.
Its going to Infinity when divide by ZERO after giving input. Make
sure loop start with 1, when division inside loop
#include <stdio.h>
int main()
{
int x,i,counter=0;
printf("Input number!\t");
scanf("%d",&x);
for(i=1;i<=x;i++)
{
if(x%i==0)
{
counter++;
}
}
if(counter<=2)
{
printf("%d is a prime number.",x);
}
else
{
printf("%d is not a prime number.",x);
}
return 0;
}
As #Some programmer dude mentioned, you can't do the x%0 because of division by 0 - this occured on first iteration.
So change your loop to starting from 1 like below:
for(i=1;i<=x;i++)
{
if(x%i==0)
{
counter++;
}
}

Take user input and continue to use it's variable throughout [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Not doing anything special. Just taking a user's input between 1 - 500, and then printing the number using for loop for each iteration. It crashes at the for loop. It does not print anything at all.
#include <stdio.h>
int forCounter() {
int num;
int count = 0;
printf("Pick a positive number (1 - 500): ");
scanf("%d", &num);
while (num < 1 || num > 500) {
printf("Out of range, try again (1 - 500): ");
scanf("%d", &num);
}
int i = num;
for (i; count <= i; count++) {
printf(count);
}
getchar();
return 0;
}
I take first input applied to 'num' and check if it's above or below allowed amount with the while loop.
When that's done it leaves and should start the for loop with i taking over for num. I tried using num in the place of i but it didn't work so I tried using a separate variable to see if it'd work.
I get two warnings seen in the image providedTwo warnings
You have to specify the format of output in printf.
int printf(const char *format, ...)
Your code:
#include <stdio.h>
int forCounter(void) {
int num;
printf("Pick a positive number (1 - 500): ");
scanf("%d", &num);
while (num < 1 || num > 500) {
printf("Out of range, try again (1 - 500): ");
scanf("%d", &num);
}
for (int i = 1; i <= num; i++) {
// printf(count); --> Bad
printf("Value = %d\n", i);
}
getchar(); // this will return immediately
return 0;
}

What's going on, why isn't this working? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Here is the code. Very basic and should work properly:
int main() {
int n, status, i;
printf("Please enter an integer bigger than 1: ");
status = scanf("%d", &n);
if (status != 1 || n > 1) {
printf("Invalid input!");
return 1;
}
for (i = 2; i <= n; i++) {
printf("What is going on %d\n", i);
}
return 0;
}
The program compiles good, and after I insert "10" as input, the program does nothing. Just as if it was supposed to return 0 without doing nothing.
EDIT: The main lesson here is don't post questions when it's late and you're super tired. The accepted answer (and handful of friendly comments) show why
Do you not see the problem here?
if (status != 1 || n > 1) { // n can not be larger than 1.
printf("Invalid input!");
return 1;
}
for (i = 2; i <= n; i++) { // n must be larger than 2.
printf("What is going on %d\n", i);
}

guess number wrongly with a %50 chance [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
In my homework user selects a number between 1 and 20 but at a chance of %50 computer can hepls wrongly.For example selected number:15 user guess:10 computer may say(guess lower number) with a %50 chance. I tried to create a chance by using another random but it selects a constant number so program always say guess higher or lower.Am I thinking wrongly ? Please help me to solve this problem.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand(time(NULL));
int randomNum = (rand() % 20) + 1;
int c = (rand() % 20) + 1;
int i = 1;
int userGuess = 0;
printf("Guess a number between 1 to 20 \n"); //start with guess
scanf(" %d", &userGuess);
while (randomNum != userGuess) {
if (userGuess > randomNum && c % 2 == 0) {
printf("Guess lower number \n");
scanf(" %d", &userGuess);
} else
if (userGuess > randomNum && c % 2 != 0) {
printf("Guess higher number \n");
scanf("%d", &userGuess);
} else
if (userGuess < randomNum && c % 2 == 0) {
printf("Guess higher number \n");
scanf(" %d", &userGuess);
} else
if (userGuess < randomNum && c % 2 != 0) {
printf("Guess lower \n");
scanf("%d", &userGuess);
}
i++;
return 0;
}
You need to get a new random number c for the 50% condition each loop.
Move the line like int c= (rand()%20)+1; into the loop so that it gets a fresh number each time.
But you don't need to do the %20 or +1 on c. You are doing % 2 in the if() statement. Don't add 1 since you are already comparing it to 0 or != 0. So something like:
while (randomNum!=userGuess)
{
int c = rand();
if(userGuess>randomNum && c%2 == 0)
...

Resources