Jump to content

showing prime;

- - - - -

  • Please log in to reply
6 replies to this topic

#1
NicholasIT

NicholasIT

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
hmm,im doing a question that give a number and showing the prime numb
eg:give 23
it will show 2,3,5,7,11,13,17,19

#include<iostream>

using namespace std;


int main()

{

	int prime=2;


	cout<<"Insert a number";

	cin>>prime;


	for(;prime>=2;prime--)

	{

		if( prime !=prime && prime % prime ==0)

			cout<<prime<<endl;

	}

	

	system("pause");

}



hmm,anyone can give some hints to do it ?

Edited by NicholasIT, 25 June 2010 - 09:46 PM.
addtion info


#2
opwuaioc

opwuaioc

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 216 posts
Well, I think you're showing quick code, right? As in, this isn't what you actually have? If so, then you don't want to define the number that you have your users entering (so remove = 2; in ln. 6). So what you're doing in your for statement as you have it (minus the first part) is using the % operand on the same number (int prime = 2; defines the variable prime, and then you're having them enter the same named variable). You need to differentiate.

For statements look more like this for(int i = 2; i < prime; i++). The if-statement can then divide prime by i and see if the remainder is zero.

As for a quick hint, for prime numbers, "i < prime" is double the effort. There's a way to cut that in half, but I'll let you see if you figure it out. I hope this puts you on the right track, but if you're still having trouble, I'll be back on to see if you get it. :)

#3
NicholasIT

NicholasIT

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
your users entering (so remove = 2; in ln. 6) <<<<
hmm,sorry not really understand about that statement ?@@

#4
opwuaioc

opwuaioc

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 216 posts
Sure:

int prime=2;

cout<<"Insert a number";
cin>>prime;

The line "int prime = 2;" is redundant because the user enters a number into a variable with the exact same name as the one you already defined.

Edit: Sorry if I may have misworded that, as I reread what I wrote, I meant that in that line, the "= 2" is redundant.
Something witty here.

#5
NicholasIT

NicholasIT

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
hmm,i try to modified but i think im not understand it.
#include<iostream>
using namespace std;

int main()
{
	
	int prime;

	cout<<"Insert a number";
	cin>>prime;

	for(int i=2;i<prime;prime--)
	{
		if(prime % i ==1)
			cout<<prime<<endl;
	}
	
	system("pause");
}


hmm,if using the true statement and false statement ?or u showing ur way?@@

#6
opwuaioc

opwuaioc

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 216 posts
I don't know why you switiched from i++ to prime-- - you're testing prime, not i. You won't get the results you want. Also, do "prime % i == 0" for your if-statement, not "== 1".
Something witty here.

#7
NicholasIT

NicholasIT

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
	int prime;

	cout<<"Insert a number";
	cin>>prime;

	for(int i=2;i<prime;i++)
	{
		if( prime % i== 1)
			cout<<prime<<endl;
		

	}
	
	


i managed to do something like this oni @@




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users