Thread: How to find the proper divisor?

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    16

    How to find the proper divisor?

    The problems says "a program will find the sum of the proper divisors of a give number n. The proper divisors on n are the numbers less than n that divides it evenly, they do not include n itself." I have no idea for this program, which function should i use?

  2. #2
    Registered User stormbringer's Avatar
    Join Date
    Jul 2002
    Posts
    90
    use modulo

    Code:
    if ((x % y) == 0)
        is divisor
    else
       ain't

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    Just loop from Div = N - 1 to 1 and check if that N % Div is zero. If it is then print Div. Feel free to use my code as an example, but you aren't allowed to use it for any educational credits.
    Code:
    /* (c) Copyright 2002 Daniel Raxter, All Rights Reserved. */
    #include <stdio.h>
    #include <stdlib.h>
    
    void ListProperDivs (int N)
    {
      int Div = N;
    
      while (--Div > 0){
        if ((N % Div) == 0)
          printf ("%d ", Div);
      }
    }
    
    int main (void)
    {
      int N;
    
      printf ("Enter a number: ");
      if (scanf ("%d", &N) != 1){
        fprintf (stderr, "Bad Input\n");
        exit (EXIT_FAILURE);
      }
      /* Okay so far */
      ListProperDivs (N);
    
      return (EXIT_SUCCESS);
    }

  4. #4
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    And you can loop through 1...n/2, since anything greater than n/2 is not possible.

    Also the code above does not find the sum. But you can implement that easily.
    Last edited by Cshot; 07-24-2002 at 10:28 AM.

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    Oops, that should teach me to read threads more carefully. But as Cshot said, it's a trivial change so I won't repost the code.

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    16
    I don't need anyone to coding for me. i need idea of this program

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    16
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void List (int N);
    int N = 0;
    void main (void)
    {
      while (N < 40)
      {
        N++;
        printf("\n%d = ", N);
        List (N);
      }
    
    }
    
    void List (int N)
    {
      int Div = N;
    
      while (--Div > 0){
        if ((N % Div)== 1 )
          printf ("%d ", Div);
      }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. How to find O of threads ?
    By jabka in forum C Programming
    Replies: 3
    Last Post: 03-11-2008, 12:25 PM
  3. ld.exe: cannot find -l-lstdc++
    By Tonto in forum Tech Board
    Replies: 3
    Last Post: 04-10-2007, 11:20 PM
  4. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 08:28 AM
  5. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 09:28 AM