Prime numbers

This is a discussion on Prime numbers within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> int main() { int count = 0; int num_limit = 100; int factor = 1; int result = ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    39

    Prime numbers

    Code:
    #include<stdio.h>
    
    int main()
    {
       int count = 0;
       int num_limit = 100;
       
       int factor = 1;
       int result = 0;
       for(num_limit = 100; count <= num_limit; count++)
       {
          if(count == 2)
          {
             printf("%d\n", count);
          }
          
          if(count == 3)
          {
             printf("%d\n", count);
          }
          
          if(count % 2 != 0 && count % 3 != 0)
          {
             while(factor <= count) // Trying to exclude perfect squares by finding the factors of number(count)
             {
                if(factor % count == 0) // Findin factors of number(count)
                {
                   int fact_res = factor * factor; // fact_res the result of multiplying a factor by itself
                   if(fact_res == count)
                   {
                      result++; // if fact_res is equal to number(count) then number is a perfect square
                   }            // then result is increased by 1
                }
                
                factor++;
             }
             
             if(result == 0)
             {
                printf("%d\n", count); // if result is greater than 0 then number is not a prime number
             }
          }
       }
    
       return 0;
    }
    It only outputs 2 and 3 then ends. Please help me out.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Step through your program with the debugger, and see what happens when the number being tested is 5 - that will show you.

    I don't clearly understand what you were trying to do here. Looks like the mod test, but then -- well, it needs some print statements added to it with getchar()'s immediately afterward, to show you what's gone off the rails.

    Please DO NOT start another thread on this!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. non prime numbers or composite numbers program.help plz!
    By danishzaidi in forum C Programming
    Replies: 10
    Last Post: 11-15-2011, 11:10 AM
  2. Prime numbers
    By cubbies5 in forum C++ Programming
    Replies: 11
    Last Post: 03-30-2011, 10:19 AM
  3. prime numbers?
    By tu_user in forum C++ Programming
    Replies: 7
    Last Post: 01-16-2004, 01:19 PM
  4. Help with prime numbers
    By Clean Killa in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2002, 04:42 PM
  5. Prime Numbers
    By Korn1699 in forum C++ Programming
    Replies: 7
    Last Post: 11-03-2001, 09:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21