How to make a prime number check with restart option in C? - 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

Related

Number guessing game always returns the same output

I'm making a simple number guessing game from 1 - 10 using an if...else statement, it worked the first time, but it seems it always outputs "You didn't guess it correctly." when I'm guessing or the other way around. Would be great for some help, so here are the solutions I made as of asking this question:
//This is the first solution I've come up with.
#include <stdio.h>
#include <stdbool.h>
int main() {
int guessingNumber = '5';
printf("Guess from 1-10: ");
scanf("%d", &guessingNumber);
if (guessingNumber == true) {
printf("You guessed it!\n");
}
else {
printf("You didn't guess it correctly.\n");
}
return 0;
}
//Here is the second one.
#include <stdio.h>
int main() {
int guessingNumber = '5';
int guess;
printf("Guess from 1-10: ");
scanf("%d", &guess);
if (guessingNumber == guess) {
printf("You guessed it!\n");
}
else {
printf("You didn't guess it correctly.\n");
}
return 0;
}
I'm sure that the problem causing this is in this lines from the first solution:
printf("Guess from 1-10: ");
scanf("%d", &guessingNumber);
if (guessingNumber == true) {
printf("You guessed it!\n");
and this from the second solution that I'm sure the problem (might also) came:
printf("Guess from 1-10: ");
scanf("%d", &guess);
if (guessingNumber == guess) {
printf("You guessed it!\n");
The comparison is unsafe int and a bool. Check the following.
int main()
{
int actualNumber, guessingNumber = 5;
printf("Guess from 1-10: ");
scanf_s("%d", &actualNumber);
if (guessingNumber == actualNumber) {
printf("You guessed it!\n");
}
else {
printf("You didn't guess it correctly.\n");
}
return 0;
}

Is my do while loop not working due to this function?

Im making a program where it asks the user to guess a number 1-100 that the computer is thinking about.
In the end of the program, when the user has guessed the correct number, im trying to get the program to ask if user wants to play again (restart the program).
To solve this, i tried using a do while loop & char repeat;. The loop is stretching from almost the beginning of the program, until the end, althought without success. Does anyone know what im doing wrong? Is it because of the function talfunktion, that the loop won't pass?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int talfunktion (int tal, int guess, int tries, char repeat);
int main () {
do {
srand(time(NULL));
int tal = rand() % 100 + 1; //tal is the correct value that the code is thinking of
int guess; //guess is the guessed value of the user
int tries = 0; // amount of tries it took until getting correct
char repeat;
printf("Psst, the right number is: %d \n", tal); // remove later, not relevant to uppg.
printf("Im thinking of a number between 1 and 100, guess which!");
printf("\nEnter: ");
scanf("%d", &guess);
guess = talfunktion(tal, guess, tries, repeat);
getchar();
getchar();
return 0;
}
int talfunktion(int tal, int guess, int tries, char repeat) {
do {
if (guess < tal) {
tries++;
printf("\nYour guess is too low, try again!");
printf("\nEnter: ");
scanf("%d", &guess);
}
else if (guess > tal) {
tries++;
printf("\nYour guess is too high, try again!");
printf("\nEnter: ");
scanf("%d", &guess);
}
} while (guess > tal || guess < tal);
if (guess == tal) {
printf("\nCongratulations, that is correct!");
tries++;
printf("\nYou made %d attempt(s)", tries);
printf("\nPlay Again? (y/n)");
scanf("%c", &repeat);
}
} while (repeat == 'y' || repeat == 'Y');
}
This is one possible solution
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void talfunktion(int tal, int guess, int* tries)
{
if (guess < tal)
{
(*tries)++;
printf("\nYour guess is too low, try again!");
}
else if (guess > tal)
{
(*tries)++;
printf("\nYour guess is too high, try again!");
}
else if (guess == tal)
{
(*tries)++;
printf("\nCongratulations, that is correct!");
printf("\nYou made %d attempt(s)", *tries);
}
}
int main (void)
{
int tal; //tal is the correct value that the code is thinking of
int guess; //guess is the guessed value of the user
int tries = 0; // amount of tries it took until getting correct
char playAgain;
do {
srand(time(NULL));
tal = rand() % 100 + 1; //tal is the correct value that the code is thinking of
printf("\nIm thinking of a number between 1 and 100, guess which!");
printf("\nEnter: ");
scanf("%d", &guess);
talfunktion(tal, guess, &tries);
printf("\nPsst, the right number is: %d", tal); // remove later, not relevant to uppg.
getchar(); //to halt the code for taking the input
if(guess == tal)
{
tries = 0;
printf("\nPlay Again? (y/n)\n");
scanf("%c", &playAgain);
}
} while (playAgain != 'n');
return 0;
}
There are several things mentioned in the comments that describe problems,
Things you should look at:
Do not define a function inside another function
be careful where you place return statements
when using character testing, use char type for variable
consider simplifying your logical comparisons. (eg guess > tal || guess < tal is the same as guess != tal )
make sure automatic variables are placed such that they are visible where used.
Place space in format specifier: " %c" for scanf() to consume newline character. (instead of excessive use of getchar())
Here is a simplified version of your code, with modified main and talfunktion functions...
char talfunktion(int tal);
int main (void) {
int tal=0;//remove from inside {...} to make it visible to rest of function
char repeat = 'n';
srand(time(NULL));
tal = rand() % 100 + 1; //tal is the correct value that the code is thinking of
do {
repeat = talfunktion(tal);
}while((tolower(repeat) == 'y'));
return 0;
}
char talfunktion(int tal)//do all relevant work in function and return
{ //only what is necessary
int guess = 0;
char repeat = 'n';
printf("Im thinking of a number between 1 and 100, guess which!");
printf("\nEnter a number from 1 to 100: ");
scanf("%d", &guess);
if((guess < 1) || (guess > 100))
{
printf("Entered out of bounds guess...\n");
}
else if (guess != tal)
{
if(guess < tal) printf("guess too small\n");
else printf("guess too large\n");
printf("Try again? <'n' or 'y'>\n");
scanf(" %c", &repeat);//space in format specifier to consume newline character
if(tolower(repeat) != 'y') return 'n';//tolower() allows both upper and lower case
}
else
{
printf("Congratulations: You guessed right.\n");
printf("Play again? <'n' or 'y'>\n");
scanf(" %c", &repeat);
}
return repeat;
}

"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

While Loop Understanding

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;
}

Resources