While Loop Understanding - c

I'm having a problem and I tried several times to solve this problem maybe you could help.
I need to modify the program below so that it asks the user to enter any number other than the number equal to the number of times they've been asked to enter a number. (i.e on the first iteration "Please enter any number other than 0" and on the second iteration "Please enter any number other than 1"m etc. etc. The program must behave accordingly exiting when the user enters the number they were asked not to.) Now the code that I have below reacts a bit differently and here's what's happening when I run it:
Enter a number: 4
Please enter a number other than 4
5
Enter a number other than 5
5
wrong
6
Enter a number other than 6
7
Enter a number other than 6
and this is my code below:
#include <stdio.h>
int main()
{
int number, x=0, counter = 0;
printf("Enter a number: ");
scanf("%d", &number);
printf("Please enter a number other than %d\n", number);
while (number!=x)
{
scanf("%d", &x);
while (x!=counter)
{
printf("Enter a number other than %d\n", x);
scanf("%d", &counter);
if (counter==x)
{
printf("wrong\n");
break;
}
}
if (number==x)
{
printf("wrong\n");
break;
}
}
return 0;
}
I really hope I explained the question correctly please let me know.

If I understood it well, you want a program that behaves like this, isn't it?
Please enter a number other than 0
4
Please enter a number other than 1
7
Please enter a number other than 2
8
Please enter a number other than 3
2
Please enter a number other than 4
4
** END OF PROGRAM **
Then, it's much more simpler than you thought...
#include <stdio.h>
int main()
{
int number, counter = 0;
do
{
printf("Please enter a number other than %d\n", counter);
scanf("%d", &number);
}
while (number != counter++);
return 0;
}
UPDATE:
#include <stdio.h>
int main()
{
int number = 0, previous;
do
{
previous = number;
printf("Please enter a number other than %d\n", previous);
scanf("%d", &number);
}
while (number != previous);
return 0;
}

Try:
#include <stdio.h>
int main()
{
int number, x=0, counter = 0;
int flag = 0;
printf("Enter a number: ");
scanf("%d", &number);
printf("Please enter a number other than %d\n", number);
while (number!=x)
{
scanf("%d", &x);
while (x!=counter)
{
printf("Enter a number other than %d\n", x);
scanf("%d", &counter);
if (counter==x)
{
printf("wrong\n");
flag = 1;
break;
}
}
if (number==x || flag=1)
{
printf("wrong\n");
break;
}
}
return 0;
}

Related

How to make a prime number check with restart option in C?

I made a programme that checks if a number is a prime number or not.
The programme worked well at the start. When the programme started, I included a prime number and it gives me the correct answer. When I restart programme with the “yes” option and give it a non-prime number it also gives me the correct answer. When I restart the programme again with “yes“ option, however, none of the numbers returned are prime, even if the input is a prime number.
Could you let me know any mistakes from my code?
This is my code
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int i=0, number, count=0;
char answer;
printf("\n\nthis programm check if a nummber is a prime nummber\n\n");
do
{
printf("input a positive nummber: ");
scanf("%d", &number);
for (i=2; i<=number/2; i++)
{
if(number%i==0)
{
count=1;
break;
}
}
if (count==0)
{
printf("\n%d is prime nummber.\n\n",number);
}
else if (count==1)
{
printf("\n %d is not prime nummber.\n\n",number);
}
do
{
printf("Do you want to restart the programm (J/N)? ");
scanf(" %c", &answer);
answer=toupper(answer);
} while (answer!='J' && answer!='N');
} while (answer=='J');
return 0;
}
The fix is very simple: Just add count = 0 at the start of the body of the outer do{}while() loop. If you forget about resetting this count variable you have an history effect from the loop iteration before.
Please use the following code :
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int i=0, number=0, count=0;
char answer;
printf("\n\nthis programm check if a nummber is a prime nummber\n\n");
do
{
printf("input a positive nummber: ");
scanf("%d", &number);
for (i=2; i<=number/2; i++)
{
if(number%i==0)
{
count=1;
break;
}
}
if (count==0)
{
printf("\n%d is prime nummber.\n\n",number);
}
else if (count==1)
{
printf("\n %d is not prime nummber.\n\n",number);
}
do
{
printf("Do you want to restart the programm (J/N)? ");
scanf(" %c", &answer);
answer=toupper(answer);
count=0;
} while (answer!='J' && answer!='N');
} while (answer=='J');
return 0;
}
I hope it will resolve your issue

Why does my count becomes 0 when my random number is greater than the number I write?

I have written C code for a game where a random number is generated and the user has to guess the correct number. So, whenever the I write a number and it is greater than the random number, my code is executing as it should. However when I write a number which is less than the random number,the count variable becomes zero, instead of decreasing the count value by 1. I cannot figure out what am I doing wrong?
I have attached the image of my output where the problem can be seen.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
main()
{
time_t t;
int a,count=5;
srand((unsigned)time(&t));
int randomnumber=rand()%21;
printf("This is a guessing game, Only 5 tries are left\n");
printf("enter the number: ");
scanf("%d",&a);
printf(" \n");
if (a>=20){
printf("ERROR!! Enter number only between 0-20\n");
}
printf("You only have %d tries left\n",count);
printf("enter number only between 0-20: ");
scanf("%d",&a);
printf(" \n");
if (a==randomnumber){
printf("You guessed it correctly, YOU WIN!!\n");
}
else{
for(count=4;count>=1;count--)
{
if (a>randomnumber){
printf("My number is smaller than that\n");
printf("You only have %d tries left\n",(count));
printf("enter number again: ");
scanf("%d",&a);
printf(" \n");
}
}
if (a<randomnumber){
printf("My number is greater than that\n");
printf("You only have %d tries left\n",(count));
printf("enter number again: ");
scanf("%d",&a);
printf(" \n");
}
if (a==randomnumber){
printf("You guessed it correctly, YOU WIN!!\n");
}
}
if (a!=randomnumber)
{
printf("You LOOSE!!, the Number was %d",randomnumber);
}
return 0;
}
Your tests for a < randomnumber and a == randomnumber are not inside the for loop. So you only check them after you've counted all the way down to 0.
You need to put all the tests inside the loop. You should also use else if so that you don't check the new number that they've entered on the same iteration (since you haven't decremented count yet).
And you need to break out of the loop when they guess the number.
The simplest way to organize this is to take all the code that prompts for the next number out of the if blocks, since it's the same for less than and greater than. The if blocks just print which way the error was, and break out when they win.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
main()
{
time_t t;
int a,count=5;
srand((unsigned)time(&t));
int randomnumber=rand()%21;
printf("This is a guessing game, Only 5 tries are left\n");
printf("enter the number: ");
scanf("%d",&a);
printf(" \n");
if (a>=20){
printf("ERROR!! Enter number only between 0-20\n");
}
printf("You only have %d tries left\n",count);
printf("enter number only between 0-20: ");
scanf("%d",&a);
printf(" \n");
if (a==randomnumber){
printf("You guessed it correctly, YOU WIN!!\n");
}
else{
for(count=4;count>=1;count--)
{
if (a>randomnumber) {
printf("My number is smaller than that\n");
} else if (a<randomnumber) {
printf("My number is greater than that\n");
} else {
printf("You guessed it correctly, YOU WIN!!\n");
break;
}
printf("You only have %d tries left\n",(count));
printf("enter number again: ");
scanf("%d",&a);
printf(" \n");
}
}
if (a!=randomnumber)
{
printf("You LOOSE!!, the Number was %d",randomnumber);
}
return 0;
}

How to display all user input numbers in C?

I am required to create a function that uses arrays and a menu system that displays:
the sum of all numbers entered, the average of numbers entered, and all numbers entered. It will allow the user to enter up to 1000 numbers.
I have the majority of the code working, I just need to figure out how to display all of the numbers a user has entered so far. Would anyone be able to help me with this? Thanks!
I've tried displaying the number entered, but this does not fit the assignment requirements.
Here is the code I have so far:
/*
Title: Array Intro
Author: James Henderson
Desc: a program designed to display the sume, average, and all previous numbers entered of user input numbers
Date: 11/06/19
*/
#include <stdio.h>
#include <math.h>
//Create Variables
//used for math
int counter = 0;
float number, sum = 0.0, average;
//user input number
int userInt;
int userInput[1000];
//Void Function
static void sumFunction(userInput)
{
printf("\n\tWelcome!\n");
printf("Enter 1 to begin:\n");
scanf("%i", &userInput);
//switch statement
while (1)
{
switch (userInput)
{
case 1:
printf("\nEnter a number:\n");
while (1)
{
scanf("%i", &userInput);
//determine sum
number = userInput;
sum += number;
counter++;
average = sum / counter;
printf("\n The average of the numbers is %.2f", average);
printf("\n The sum of the numbers is %.2lf", sum);
printf("\n You may enter up to 1000 numbers");
printf("\n You have entered %d numbers\n", counter);
if (counter == 1000)
{
printf("\nThank you for using my program! Have a lovely day :)");
return;
}
}
}
}
}
Your approach was right, just look how i used userInput. Below code works fine:
#include <stdio.h>
#include <math.h>
//Create Variables
//used for math
int counter = 0;
float number, sum = 0.0, average;
//user input number
int userInt;
int userInput[1000];
//Void Function
static void sumFunction()
{
printf("\n\tWelcome!\n");
printf("Enter 1 to begin:\n");
scanf("%i", &userInt);
//switch statement
while (1)
{
switch (userInt)
{
case 1:
printf("\nEnter a number:\n");
while (1)
{
scanf("%i", &userInput[counter]);
//determine sum
number = userInput[counter];
sum += number;
counter++;
average = sum / counter;
printf("\n The average of the numbers is %.2f", average);
printf("\n The sum of the numbers is %.2lf", sum);
printf("\n You may enter up to 1000 numbers");
printf("\n You have entered %d numbers\n", counter);
// number entered so far
printf("\n The list of numbers entered so far : \n");
for(int i=0;i<counter;i++){
printf(" %d ",userInput[i]);
}
printf("\n");
if (counter == 1000)
{
printf("\nThank you for using my program! Have a lovely day :)");
return;
}
}
}
}
}
int main(){
sumFunction();
}

"Guess The Number" In C But If Statements Don't Work Right

I have a program in C that the user runs to play a "Guess The Number" game. It runs correctly to start but after the user enters 2 numbers (1 for the initial and 1 for the try again) the program repeats when it is supposed to have a limited number of tries.
Here is my code for the program:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
//----Guess a Number Game.-----------------------------------------
// srand and rand needs the #include <stdlib.h> library
srand(time(NULL)); //seeding the guess a number game with the system time, so the guess a # game always starts at a different point.
int guess;
int correctnum;
correctnum = rand();
printf("Enter a number:");
scanf("%i",&guess);
if(guess>correctnum) // If aa is greater than bb AND aa is greater than cc.
{
printf("Please enter another number, lower this time!");
scanf("%i",&guess);
main();
}
else if (guess<correctnum)
{
printf("Please enter another number, higher this time!");
scanf("%i",&guess);
main();
}
else if (guess==correctnum)
{
printf("You are a WINNER!\n");
printf("You guessed the number right and it was %i!\n",correctnum);
}
int repeat;
printf("Would you like to play again? 1=Yes and 2=No.");
scanf("%i",&repeat);
if(repeat==1)
{
main();
}
if(repeat==2)
{
printf("Hope you had a good time playing the game! See you soon!\n");
return 0;
}
}
Don't call main() recursively. What you need is a simple loop. Something like:
int main(void) {
srand(time(NULL));
int correctnum = rand();
int guess;
int done = 0;
while (! done) {
printf("Enter a number:");
scanf("%i",&guess);
if (guess>correctnum) {
printf("Please enter another number, lower this time!");
} else if (guess<correctnum) {
printf("Please enter another number, higher this time!");
} else /* if (guess==correctnum) */ {
printf("You are a WINNER!\n");
printf("You guessed the number right and it was %i!\n",correctnum);
done = 1;
}
}
}
You should probably check for errors from scanf() too, but first things first.
you can implement all functions you want in a loop:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void main(){
srand(time(NULL));
int guess;
int correctnum;
correctnum = rand() % 100; #it's better to get a number between 1~100
int done = 1;
printf("guess the number:\t");
scanf("%i", &guess);
while(done){
if(guess < correctnum){
printf("Please enter another number, higher this time:\t");
scanf("%i", &guess);
}
else if(guess > correctnum){
printf("Please enter another number, lower this time:\t");
scanf("%i", &guess);
}
else if(guess == correctnum){
printf("well done! if you want to play again, input 1, else input 0:\t");
scanf("%i", &done);
if(done == 1){
correctnum = rand() % 100;
printf("guess the number:\t");
scanf("%i", &guess);
}
}
}
printf("Hope you had a good time playing the game! See you soon!\n");
}

Two questions, why the double print and how to start it over again! C programming

I'm writing the simplest game in C - Number guessing game. The game itself works good. Yaay me. The problem is that I don't know how to start it over. See code below:
int main()
{
int number, innum, times = 0;
char playAgain;
srand((unsigned)time(NULL));
number = 5;//rand() % 1000;
for(;;)
{
while(innum != number)
{
printf("Enter a number: ");
scanf("%d", &innum);
if(innum > number)
printf("The entered number is too big!\n");
if(innum < number)
printf("The entered number is too small!\n");
times++;
if(innum == number)
{
printf("Congrats you guessed right!\n");
printf("It took you %d tries\n", times);
}
}
printf("Do you want to play again?");
scanf("%c", &playAgain);
if(playAgain == 'n')
break;
}
return 0;
}
The first problem is that it prints "Do you want to play again?" two times. Why is that? And the other problem is, how do I get the game to start again?
Thanks in advance.
This should work for you:
(What i did? Added a space by the scanf and put the declaration of number, times and innum in the for loop)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int number, innum, times;
char playAgain;
srand((unsigned)time(NULL));
for(;;) {
/*Declare the variables here*/
number = 5; //rand() % 1000;
innum = 0;
times = 0;
while(innum != number) {
printf("Enter a number: ");
scanf("%d", &innum);
if(innum > number)
printf("The entered number is too big!\n");
if(innum < number)
printf("The entered number is too small!\n");
times++;
if(innum == number) {
printf("Congrats you guessed right!\n");
printf("It took you %d tries\n", times);
}
}
printf("Do you want to play again?");
scanf(" %c", &playAgain);
//^Added space here to 'eat' any new line in the buffer
if(playAgain == 'n')
break;
}
return 0;
}
possible output:
Enter a number: 2
The entered number is too small!
Enter a number: 6
The entered number is too big!
Enter a number: 5
Congrats you guessed right!
It took you 3 tries
Do you want to play again?y
Enter a number: 3
The entered number is too small!
Enter a number: 5
Congrats you guessed right!
It took you 2 tries
Do you want to play again?n

Resources