Thread: to print prime nymbers upto 300

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    4

    to print prime nymbers upto 300

    Code:
    #include<conio.h>
    #include<stdio.h>
    void main(void)
    {
         int num,i;
    clrscr();
    for(num=1;num<=300;num++)
    {
          for(i=1;i<=num;i++)
           {
              if(i!=1 && i!=num && num%i==0)
              break;
              else if(i<num)
              continue;
              else
              printf("%4d",num);
            }
    }
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Again, do you have a question?

    The inner loop is over-complicated (simplest would be to call a function like is_prime and implement that more simply) and 1 is not a prime.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Maybe the OP is shy?
    You've got a void main. You should alway use int main.
    Also, you might want to google Sieve of Eratosthenes, it's a neat algorithm for finding prime numbers.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-19-2009, 10:32 PM
  2. prime number program with function
    By mackieinva in forum C Programming
    Replies: 17
    Last Post: 09-20-2007, 08:36 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM