• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

Write a program that ask user to guess a prime number in 5 tries?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
#include<iostream>
using namespace std;
//I tried this but it does not run. How should I check guess is prime number or not?

bool isprime(int guess)
{

for (int i=0; i<=guess; i++)

if((guess%i != 0)||(i != 0)||(i!=guess))
{
return false;
}
else
{
return true;
}
}
int main()
{
const int limit = 5;
int attempt;
for(attempt = 0; attempt < limit; attempt++)
{
cout<<"Please enter any Prime number brtween 1 to 100. You have "<<(limit-attempt)<<" attempts remaining"<<endl; >
int guess;
cin>>guess;
if(isprime(guess))
break;
}
if(attempt == limit)
cout<<"You loose"<<endl; >
else
cout<<"You Winner"<<endl; >
Improve solution   Permalink        Posted 41 mins ago
Ehtisham Ul Hassan
Have a Question or Comment?
Add your solution here
 
Rancher
Posts: 291
11
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ehtisham - please could you use code tags so we can read your code? There's help on how to use them here:
https://coderanch.com/wiki/659781/Code-Tags
 
Sheriff
Posts: 16067
267
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is here:

 
John Matthews
Rancher
Posts: 291
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Junilu.

So Ehtisham, I suggest you work through your code with a test value, say 7 which we know is a prime.

In isprime() argument guess is 7, and when i=0, the condition (i!=guess) is true so the function returns false; not a good start

You need to modify your test(s), and check them by hand until you are happy they give the correct result for a few known primes and non-primes. For starters, you don't know if a number is prime or not until you have tested for division by all numbers in the for() range, so I don't think you can return true inside the loop.
 
Marshal
Posts: 72066
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

There is more wrong with your loop. Don't start by dividing by 0 (see lines 4‑5 in Junilu's post). Don't loop up to ≤ i, which is an out by one error. Work out how many times the loop will executed.
Anyway, you don't need to test all numbers to see whether they divide into a number. You need to test prime numbers ≤ √i. Even if you don't have a supply of prime numbers to iterate, you can use 2 and then all subsequent odd numbers. That will speed up execution a lot.
 
Junilu Lacar
Sheriff
Posts: 16067
267
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The for-loop isn't guaranteed to execute at all so there's a possibility that execution will not go through the if-else statement inside the for-loop and hit one of the return statements. In that case, there is no statement after the for-loop that would return a boolean value. Unlike with Java which would produce a compile-time error, C/C++ is more permissive and will let that code compile. I don't know what happens at runtime though if the for-loop doesn't execute at least one iteration.
 
Ehtisham Ul Hassan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have completed this problem. Thanks for helping ❤

#include <iostream>
using namespace std;
bool is_prime(int guess)
{
if ( guess < 2 || guess > 100)
return false;
int n=2;
while(n<guess)
{
if ( (guess % n ) == 0)
return false;
n++;
}
return true;
}
int main()
{
const int limit = 5;
int attempt ;
while ( attempt < limit)
{
cout << "Please enter any Prime number between 1 and 100, you have " <<
(limit- attempt) << " tries remaining." << endl;
int guess;
cin >> guess;
attempt++;
if ( is_prime(guess) ) break;
}
if (attempt == limit)
cout << "You loose noob" << endl;
else
{
cout << "Winner Winner Chicken Dinner" << endl;
cout<<"You guess the Prime number by taking "<<attempt<<" tries.";}
}
 
straws are for suckers. tiny ads are for attractive people.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic