6 Replies - 11641 Views - Last Post: 22 February 2009 - 07:52 AM Rate Topic: -----

#1 manoj_kr1976  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-February 09

prime no between 1 to 100 in c language

Post icon  Posted 22 February 2009 - 04:38 AM

how i display prime no between 1 to 100 ?
Is This A Good Question/Topic? 0
  • +

Replies To: prime no between 1 to 100 in c language

#2 DoubleFission  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 223
  • Joined: 20-September 08

Re: prime no between 1 to 100 in c language

Posted 22 February 2009 - 04:46 AM

Well first you have to work out how a single number is a prime number (If it has no factors except 1 and itself).

Then you just have a for loop which goes through 1 to 100, printing it out when done...

Simple
Was This Post Helpful? 0
  • +
  • -

#3 manoj_kr1976  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-February 09

Re: prime no between 1 to 100 in c language

Posted 22 February 2009 - 04:57 AM

View Postmanoj_kr1976, on 22 Feb, 2009 - 03:38 AM, said:

how i display series of prime no between 1 to 100 in c language?

Was This Post Helpful? 0
  • +
  • -

#4 manoj_kr1976  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-February 09

Re: prime no between 1 to 100 in c language

Posted 22 February 2009 - 05:52 AM

main()
{
    int x,y;
    for (x=1;x<=100;x++)
    {
        for(y=2;y<x;y++)
        {
            if(x%y==0)
            {
                printf("%d",x);
            }
        }
     }
     getch();
}


EDIT: Added [ code ] tags
Was This Post Helpful? 0
  • +
  • -

#5 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: prime no between 1 to 100 in c language

Posted 22 February 2009 - 06:29 AM

[quote name='manoj_kr1976' post='550052' date='22 Feb, 2009 - 05:09 AM']
[quote name='manoj_kr1976' post='550046' date='22 Feb, 2009 - 04:59 AM']
main()
{
int x,y;
for(x=1;x<=100;x++)
{
for(y=2;y<x;y++)
{
if(x%y==0)
printf("%d",x);
}
}




thats is bro!!!

try if it is working, because I am not sure cause I haven't run it!!!
if the problem persists, plzzz let me know.
I'll try furthur..

This post has been edited by pdkharkar: 22 February 2009 - 06:31 AM

Was This Post Helpful? 0
  • +
  • -

#6 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: prime no between 1 to 100 in c language

Posted 22 February 2009 - 06:59 AM

Please don't double post you questions.

Here is a solution using the sieve of eratothenes.
int main(int argc, char * const argv[])
{
	const long maxnumber=10000000;
	
	
	bool numbers[maxnumber];
	
	for(long a=0; a<maxnumber; a++)
	{
		numbers[a]=true;
	}

	for(long x=2; x<maxnumber; x++)
	{
		if ( numbers[x] )
		{
			
			
			for (long y=2; y<maxnumber/x; y++)
			{
				numbers[y*x]=false;
			}
			
		}
	}
	
	for (long b=0; b<maxnumber; b++)
	{
		cout << numbers[b] << endl;
	}
	return 0;
}

I originally used vectors for infinite storage capacity.
Hope this helps!!
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,520
  • Joined: 23-August 08

Re: prime no between 1 to 100 in c language

Posted 22 February 2009 - 07:52 AM

Topics merged.

Please make every attempt to not double-post your questions.

Thanks.

EDIT: Also trimmed repeated posts.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1