Thread: Program the prints prime numbers

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    14

    Program the prints prime numbers

    I need to make a program that prints all prime numbers between 3 and 100.
    Here is my code so far. Right now it only outputs the number 3. What is wrong with it? Thanks for any help.

    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {int num=3, checknum, remainder, answer=1;
    	do{checknum=2;
    		while(checknum<num-1)
    		{remainder=num%checknum;
    		if(remainder==0)
    		{answer=0;
    		continue;}
    		else
    		answer=1;
    		checknum++;
    		}
    		if(answer=1)
    		{cout<<""<<num;}
    		num++;
    	}while(num<101);
    	return 0;
    }
    Last edited by cloudstrife910; 09-22-2010 at 02:45 PM. Reason: forgot tags

  2. #2
    Registered User
    Join Date
    Mar 2008
    Location
    Coimbra, Portugal
    Posts
    85
    Please!...My eyes hurt!...Indent your code!! Put code tags around it!

    Hurry, I think I won't survive it...

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    14
    Im sorry lol, I just copied and pasted it here, and it came out like that. I've never posted here before and I'm pretty new to programing

  4. #4
    Registered User
    Join Date
    Mar 2008
    Location
    Coimbra, Portugal
    Posts
    85
    Quote Originally Posted by cloudstrife910 View Post
    Im sorry lol, I just copied and pasted it here, and it came out like that. I've never posted here before and I'm pretty new to programing
    Well then, edit your first post and try to do your best, please. Indent how you like, but code tags are mandatory (haven't you read the forum post that talks about that? You know, the one with that title??)

  5. #5
    Registered User
    Join Date
    Mar 2008
    Location
    Coimbra, Portugal
    Posts
    85
    Quote Originally Posted by cloudstrife910 View Post
    here I'll try and rewrite it

    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {int num=3, checknum, remainder, answer=1;
    	do{checknum=2;
    		while(checknum<num-1)
    		{remainder=num%checknum;
    		if(remainder==0)
    		{answer=0;
    		continue;}
    		else
    		answer=1;
    		checknum++;
    		}
    		if(answer=1)
    		{cout<<""<<num;}
    		num++;
    	}while(num<101);
    	return 0;
    }
    now will you help? pleassse?
    Meh, here's how I would do it:

    Code:
    #include<iostream>
    using namespace std;
    
    int main() {
            int num=3, checknum, remainder, answer=1;
    	do {
                    checknum=2;
            	while(checknum<num-1) ;
    	        {
            	        remainder=num%checknum;
    			if(remainder==0) {
    	                        answer=0;
    			        continue;
    	                } else
    			        answer=1;
    	
    			checknum++;
    		}
    		if(answer=1) {
    	                cout<<""<<num;
    	        }
    			num++;
    	} while(num<101);
    
    	return 0;
    }
    Now, while I was indenting, I realized that your code is a total mess. I am quite sure that it does not do anything you want at all. Can you try and explain what you wanted to do with each line or set of lines in it?

    Hint: You have a while loop that does nothing, since it has an extra comma in front of it. It's highlighted.
    Last edited by Jorl17; 09-22-2010 at 02:53 PM.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    31
    There are two issues.

    First, replace the "continue" with "break"; second, replace "if(answer=1)" with "if (answer == 1)".

    Then it works perfect.

    Edit: Helpful info! By using "continue", you are forcing it to stay in the loop when it first enters, so the program becomes a dud before anything happens. The "break" command is used to exit loops. And the unary equals was just a typo.
    Last edited by frog; 09-22-2010 at 02:59 PM.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    14
    Code:
    #include<iostream>
    using namespace std;
    
    int main() {
            int num=3, checknum, remainder, answer=1;
    	do {
                    checknum=2;
            while(checknum<num-1) //check if the number is between 3 and 100
            {
                    remainder=num%checknum; //this will tell me if check num will divide evenly into it
    		if(remainder==0) { //here if it does then I want the while loop to end and make answer=0;
                            answer=0;
    		        continue;
                    } else
    		        answer=1;
    
    		checknum++;
    	}
    	if(answer=1) {
                    cout<<""<<num; //here if answer still equals 1 at the end of the loop I want it to print the number because the means it is prime
            }
    		num++;
    	} while(num<101);
    
    	return 0;
    }

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    14
    also that extra comma wasn't in my original code

  9. #9
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    You could use Sieve of Eratosthenes, and actually, at least one of the other language versions of this very wiki site has a working C++ code as example.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recuration Prime Numbers
    By megazord in forum C Programming
    Replies: 17
    Last Post: 05-17-2010, 08:56 AM
  2. Prime Numbers
    By Ahmed29 in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2008, 10:54 AM
  3. Help with program code re prime numbers
    By Anna Lane in forum C Programming
    Replies: 3
    Last Post: 11-16-2002, 10:48 AM
  4. Replies: 2
    Last Post: 09-11-2002, 05:00 PM
  5. Prime Numbers
    By cmangel518 in forum C++ Programming
    Replies: 13
    Last Post: 04-30-2002, 11:51 PM