GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Feb-2009, 19:50
8100 Power 8100 Power is offline
New Member
 
Join Date: Sep 2008
Posts: 24
8100 Power is on a distinguished road
Thumbs up

While loop help!


Ok, I'm writing a program to determine if the number entered is prime. So, here is my main script:

CPP / C++ / C Code:
cout << "Please enter digits to determine if prime or not, use 0 to quit"<< endl;
    cin >> input;
    
    while( input != 0 && input > 0){
       
       cin >> input;
       
       while(input % 2){   // <---This is my problem?? reference below!
          
          if ( input == 1)
              cout << "The Number is Prime" << endl;
          else
              cout << "The Number is not prime" << endl;
       }
             
    }      


You can see where I pointed out my problem ( I think). Can I input a arthmatic operator here to process every # inputted to determine the if condition?

Basically, every prime number modulous 2 puts out a 1, so I can determine if the # is prime or not by the if condition.

Thanks for any help.
  #2  
Old 09-Feb-2009, 22:21
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: While loop help!


Quote:
Originally Posted by 8100 Power
Basically, every prime number modulous 2 puts out a 1...
This is not entirely correct. While it is true that even numbers are not prime (depending upon whether 2 is considered prime or not...), for all values x where x % 2 == 1 really is just another definition of the odd number sequence.

Consider the value 9. 9 % 2 == 1, but 9 is not prime.
  #3  
Old 09-Feb-2009, 23:15
8100 Power 8100 Power is offline
New Member
 
Join Date: Sep 2008
Posts: 24
8100 Power is on a distinguished road

Re: While loop help!


Ok.

2 questions:

1. What would be the most simplest and correct way to determine if the number is prime?

2. Is my code correct?
  #4  
Old 09-Feb-2009, 23:41
8100 Power 8100 Power is offline
New Member
 
Join Date: Sep 2008
Posts: 24
8100 Power is on a distinguished road

Re: While loop help!


As I've wrote my code a little different, I've tested all numbers and everything is coming out as expected. Now, I just need to integrate my while loops. What is wrong (programming wise) with my code?
  #5  
Old 10-Feb-2009, 00:11
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 802
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: While loop help!


What code?
You know, finding prime has been covered here before.
It might help you to search the forum for previous threads.
  #6  
Old 10-Feb-2009, 09:21
8100 Power 8100 Power is offline
New Member
 
Join Date: Sep 2008
Posts: 24
8100 Power is on a distinguished road

Re: While loop help!


Quote:
Originally Posted by Howard_L
What code?
You know, finding prime has been covered here before.
It might help you to search the forum for previous threads.


I'm now asking for help about my while loop, not process of determining a prime number.

The code in the first reply if you would of read it.
  #7  
Old 10-Feb-2009, 10:31
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: While loop help!


Quote:
Originally Posted by 8100 Power
What would be the most simplest and correct way to determine if the number is prime?
For any number n, n is prime if all prime numbers less than n are found to not be factors of n.
Quote:
Is my code correct?
No. Your code asks for user input twice followed by checking to see if the last value received is odd & then outputs strings based on whether the value is 1 or not. This does not follow the definition of prime numbers.
  #8  
Old 10-Feb-2009, 20:55
8100 Power 8100 Power is offline
New Member
 
Join Date: Sep 2008
Posts: 24
8100 Power is on a distinguished road

Re: While loop help!


Quote:
Originally Posted by ocicat
For any number n, n is prime if all prime numbers less than n are found to not be factors of n.

No. Your code asks for user input twice followed by checking to see if the last value received is odd & then outputs strings based on whether the value is 1 or not. This does not follow the definition of prime numbers.

I'm still tying to register that sentence in my head. Could you elaborate?

As for my while structure. When the user enters the values, shouldn't that be in the outer loop?
  #9  
Old 10-Feb-2009, 21:57
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 802
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: While loop help!


Quote:
The code in the first reply if you would of read it.
Sorry but you said:
Quote:
As I've wrote my code a little different
Does the new code still get user input twice?
Does the new code still rely on this to determine a prime number? :
CPP / C++ / C Code:
while(input % 2)
Hopefully not. So you see, it would be helpful if you would post your newest code. That's all I was trying to say...
  #10  
Old 10-Feb-2009, 22:19
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: While loop help!


Quote:
Originally Posted by 8100 Power
I'm still tying to register that sentence in my head.
A prime number is only divisible by 1 & itself.

So look at a number of examples:
  • The reason 9 is not prime is because it can be factored to 3 * 3.
  • All even numbers are divisible by 2, & given that 2 is prime, this means that all even numbers cannot be prime (Again, primes are divisible only by themselves & 1...).
  • 11 is only divisible by itself & 1, so 11 is a prime number.
  • 21 is not prime because it is divisible by 3 & 7.
So you should beginning to understand that odd numbers aren't necessarily prime. Some are; others aren't.
Quote:
As for my while structure. When the user enters the values, shouldn't that be in the outer loop?
That is one alternative. You could also exit when the input is not numeric or negative, however, this is not overly pertinent to determining whether a value has a prime factorization or not.
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Classic recursion game of 8 Queens errors BKizzle77 Java Forum 11 11-Aug-2008 01:35
Exiting an infinite loop. memmudu C++ Forum 1 31-Dec-2007 07:25
breaking an infinite loop through stdin pisuke C Programming Language 9 03-Jul-2007 21:19
Text-Based Roulette Game mfm1983 C++ Forum 5 29-Nov-2006 13:20
messy loop help please sammacs C Programming Language 6 26-Nov-2004 16:18

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:25.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.