Thread: factors

  1. #1
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501

    factors

    what is wrong with this code, it does not return the desired output.

    Code:
    #include <stdio.h>
    
    void main() {
            int inp, div;
    
            printf("What number do you want the factors of?\n");
            scanf("%d", &inp);
            for(div=2;div * div < inp; div++) {
                    if(inp % div == 0)
                            printf("%d X %d", div, inp / div);
            }
            printf("\n");
    }

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    1) use int main() instead of void main()
    2) put a newline in your printf so the printout doesn't run together
    3) make 'div * div < inp' into 'div * div <= inp' so that square roots are counted, too.

  3. #3
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    was the void main causing the error, the other stuff wasn't. btw thanx

  4. #4
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    No, #1 wasn't causing the error - void main() is just bad coding practice. #2 made it ugly, and #3 made it not work. (I'm referring to the errors associated with each)
    Away.

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    It is not the code which is the problem, but the algorithm. When factorising you should search for the smallest factors first and divide the number to factorise by that number. Then check if the result is a prime. If not, then find its smallest factor and divide the result by its smallest factor. Repeat this proces until the result of the division is a prime.

    For example, take the number 50. This is not a prime number and its smallest divisor is 2. Now 50 / 2 = 25. Also 25 is not a prime number, its smallest divisor is 5. Now 25 / 5 = 5. Number 5 is a prime number, so the factorisation is: 50 = 2 x 5 x 5.

    Code:
    #include <stdio.h>
    
    int main() 
    {
      int inp, div;
    
      printf("What number do you want the factors of?\n");
      scanf("%d", &inp);        
    		        
      while (div != -1)
      {
        div = find_smallest_divisor (inp);
    
        if (div == -1)
        {
          /* No smallest divisor found, so current value of
              inp is a prime number and therefore it is the last
              factor. */
          printf ("%d\n", inp);
        }
        else
        {
          /* Smallest divisor found, so current value of inp is not
              a prime number and can be divised by div. */
              printf ("%d X ", div);
          inp /= div;
        }
      }
            
      return 0;
    }
    
    int find_smallest_divisor (int i)
    {
      /* Find the smallest divisor of a value i. */
    	
      int j;
      int r = -1;
    	
      for (j = 2; j < i && r == -1; j++)
      {
        if (i % j == 0)
        {
          r = j;
        }
      }
    		
      return r;
    }

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    18
    Code:
    #include <iostream.h>
    #include <conIO.h>
    
    int main(void)
    {
       clrscr();
    
       unsigned int num;
    
       cout<<"Enter number to find primes of:";
       cin>>num;
    
       for(int c=1;c<=num;c++)
       {
          if(num%c==0)
             cout<<" "<<c;
       }
       getch();
       return 0;
    }
    This may not be good programming techniques but it works and dos can handle
    I need MONEY more than help with My C++ so yeah you get the idea

    C notes preferably LOL

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >This may not be good programming techniques
    I agree, seeing as how you're using C++.

    >but it works
    Not if you compile it as C.

    >and dos can handle
    DOS is not the only operating system around, and certainly not the most popular. A program like this can easily be made portable.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-19-2009, 10:32 PM
  2. Replies: 3
    Last Post: 03-29-2005, 04:24 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Help with homework please
    By vleonte in forum C Programming
    Replies: 20
    Last Post: 10-13-2003, 11:16 AM