Thread: Please Help!

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

    Please Help!

    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;
    }
    Program for finding the first 100 prime numbers.
    It only outputs 2 and 3 then ends. Please help me out.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed