prime number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gaurav1430
    New Member
    • Nov 2007
    • 3

    #1

    prime number

    i need a simple program to write prime numberts from 1 to 100
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Originally posted by gaurav1430
    i need a simple program to write prime numberts from 1 to 100
    What have you tried so far on this problem? We are not allowed to write the whole code for you (Posting Guidelines )

    Regards

    Comment

    • gaurav1430
      New Member
      • Nov 2007
      • 3

      #3
      Originally posted by zodilla58
      What have you tried so far on this problem? We are not allowed to write the whole code for you (Posting Guidelines )

      Regards
      [code=c]
      #include<stdio. h>
      #include<math.h >

      int main()

      {
      int n,i,limit;
      printf("The prime numbers between 50 and 100 are:");
      for(n=50;n<100; n++)
      {
      limit=sqrt((dou ble)n+1);

      for (i=3; i<=limit; i+=2)
      {
      if (n%2==0) break;
      if (n%i==0) break;
      printf("\n%d",n ); break;
      }

      }
      printf("\n");
      }[/code]
      i have tried this one only. but i m getting some non prime numbers in the output plz bct them
      Last edited by sicarie; Jan 21 '08, 01:25 PM. Reason: Code tags

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        The trouble is the printf statement inside your inner for loop. The if statements before are working to break out of the loop when a non-prime number has been found, but just because a number passes the test the first time, you print it out and break out of the loop anyway. Rethink the logic here - consider using a boolean variable. Or even consider using a function isPrime(int a) to indicate if a is prime or not.

        Comment

        • manontheedge
          New Member
          • Oct 2006
          • 175

          #5
          I recommend looking into the modulus operator ( % )

          with this, you can do something like...

          7 % 4 ... which is essentially 7 divided by four ... and the remainder is returned

          so if you have

          int x = 7 % 4

          x would be 3

          you can write code to come up with prime numbers as big as you want using this, and by using literally just a few lines of code in a for loop

          Comment

          • ketand1
            New Member
            • Nov 2007
            • 5

            #6
            Originally posted by gaurav1430
            i need a simple program to write prime numberts from 1 to 100
            <Code removed - please read our Posting Guidelines.>

            Comment

            • gursimar123
              New Member
              • Jan 2008
              • 1

              #7
              code removed - read the Guidelines of the site, please

              Hiiii Dear, i have tried to find program for prime numbers from 1 to 100, u can try this program. If u want further guidance, u can also contact me , I m Gursimar, my mobile no. is removed

              Comment

              Working...